all groups > sql server odbc > january 2004 >
You're in the

sql server odbc

group:

How can I check if table already exists in a DB?


How can I check if table already exists in a DB? GTi
1/24/2004 12:44:51 PM
sql server odbc:
How can I check if table already exists in a DB?
Today I just create my table at startup, if it exist I get a error telling
me that the table already exist, ignoring the error message.
But this is a dirty way of doing it. Any other idea.
It must be quick and clean.

Re: How can I check if table already exists in a DB? Furer Alexander
1/25/2004 6:06:55 PM

Hi !
[quoted text, click to view]

If you generate sql script for table and check "generate drop object" on ,
you will see in generated scipt something like this :

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

where 'accounts' is my tablename.

You can use this approach or , using odbc API , try to get table metadata,
i'm sure it should be way to get it, just never tried it.


Regards,Alexander

Re: How can I check if table already exists in a DB? Johan Svensson
2/1/2004 3:12:27 PM
Or try this:

USE [YourDB]
IF EXISTS (
SELECT name
FROM sysobjects
WHERE type = 'u' AND
name = N'YourTable' -- Remove N if not using unicode
)
BEGIN
-- Do action
END


Regards,
Johan


[quoted text, click to view]
use SQLTables function

[quoted text, click to view]

Re: How can I check if table already exists in a DB? GTi
2/1/2004 5:04:45 PM
Thanks to you all...

[quoted text, click to view]

Re: How can I check if table already exists in a DB? Furer Alexander
2/1/2004 5:51:47 PM
use SQLTables function

[quoted text, click to view]

Re: How can I check if table already exists in a DB? Furer Alexander
2/5/2004 11:21:43 AM
IF OBJECTPROPERTY ( object_id('authors'),'ISTABLE') = 1
print 'Authors is a table'

[quoted text, click to view]

AddThis Social Bookmark Button