Thursday, August 31, 2006

Checking if different database objects exist

To check if a database exists:

if db_id('MyDatabase') is null
select 'false'
else
select 'true'


To check if a temporary table exist, use the following code. You need to search for them in tempdb because that's where all temporary tables are stored.

if object_id('tempdb..#temp', 'U') is not null
drop table #temp

GO

No comments: