Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if SQL table exists

Status
Not open for further replies.

MDA

Technical User
Jan 16, 2001
243
US
Hi all,

New challange for me... I am trying to figure out how to check if a table exists in a SQL server database using an ado connection.

I have syntax such as:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MYTABLE]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MYTABLE]... etc etc

However, I dont want to drop the table I just want to prompt a message to the user in VB that the table already exists.. I am not sure how to return something so that I know if it exists or not..

For Instance if exists then 1 otherwise 0. Then I can execute a message based on that result.

Any ideas on how this can be accomplished??

Thanks a million for everyones support.

Regards,

MDA
 
How about :-

cSql = "SELECT name FROM sysobjects WHERE name = 'MyTable'"
Set rs = Db.OpenResultset( etc etc - make ADO recordset)
If Not (rs.EOF and rs.BOF) Then
'Table name exists, show user message
Else
'Table does not exist
Endif

Hope this helps.

Regards, Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top