Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Check whether table exists

Author  Topic 

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-06-06 : 05:39:48
How can I check whether an table exists?

For example I try this, but doesn't work

when exists Test then
--do something
else
-- do something else

nr
SQLTeam MVY

12543 Posts

Posted - 2007-06-06 : 05:42:32
if exists (select * frmo sysobjects where name = 'mytable' and xtype = 'U')

if object_id('mytable') is not null

if exists (select * from information_schema where table_name = 'mytable')

You should never have to do this other than when releasing some updates.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-06-06 : 05:45:26
thnx
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-06 : 05:49:54
[code]if exists (select * from information_schema.tables where table_name = 'mytable')[/code]


KH

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-06 : 05:59:00
[code]If objectproperty(object_Id('mytable'), 'IsUserTable') IS NOT NULL
print 'table exists'
else
print 'table does not exists'[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-06-06 : 07:33:10
what means objectproperty?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-06 : 07:50:37
see http://msdn2.microsoft.com/en-us/library/ms176105.aspx


KH

Go to Top of Page
   

- Advertisement -