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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a table exists in the database

Status
Not open for further replies.

dataforums

Programmer
May 3, 2005
25
US
Hi,
I am trying to create dynamic tables but I want to check if they already exists in the database. Can anyone please tell me how to check if a table already exists in a sql server database??

Thanks in advance.

 
Try this:

Code:
select * from dbo.sysobjects where id = object_id(N'[dbo].[TableName]')



Catadmin - MCDBA, MCSA
"If a person is Microsoft Certified, does that mean that Microsoft pays the bills for the funny white jackets that tie in the back???
 
Select(object_id('tbl_TABLE'))

If this returns NULL then the table doesn't exist. If it returns a value then the table does exist.


Hope this helps


- DBAWinnipeg
 
You mean something like this?

if exists (select * from dbo.sysobjects where name = 'tablename')
begin
....
end
 
I'd do it like this:

Code:
If exists (select * from information_schema.tables where table_name = 'Table')
begin
...
end


Fi.

"The question should be, is it worth trying to do, not can it be done"


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top