Saturday, June 13, 2009

Check if database exists?


-- check db exist 1
select db_id('dbname')

-- check db exist 2
if not exists(select * from master.dbo.sysdatabases where name = 'dbname')
print 'Database does not exist.'
else
print 'Database exist.'

-- check db exist 3
if not exists(select * from sys.databases where name = 'dbname')
--create database testing
print 'Database does not exist.'
else
print 'Database exist.'

-------- check table and store procedure exist --------
use qnet
if object_id('sp_name', 'P') is not null -- for sp
print 'store procedure exist.'
else
print 'store procedure not exist.'

if object_id('tb_name', 'U') is not null -- for table
print 'table exist.'
else
print 'tabel not exist.'
--------


Reference

No comments:

Post a Comment