數據庫
法(一):
select * from master.dbo.sysdatabases where name='數據庫名'
法(二):
if DB_ID('testdb') is not null -- 如果這個數據庫已經存在了
drop database testdb;
create database testdb;
表對象
select count(*) from sysobjects where id = object_id('數據庫名.dbo.表名')
if exists (select count(*) from sysobjects where id = object_id('數據庫名.dbo.表名'))
print '存在'
else
print '不存在' 表中字段
if exists(select * from syscolumns where name='colname1' and id=object_id('數據庫名.dbo.表名'))
print '存在'
else
print '不存在'(代表表tablename1中存在colname1字段 )
例:select * from syscolumns where name='Test' and id=object_id('dbo.test') 存儲過程或視圖
if object_id('視圖或存儲過程名') is not null
drop proc/view 。。。 go
create proc/view 。。。
或
if Exists(select * from sysobjects where name='視圖或存儲過程名' AND type = 'P/V')
drop proc/view 。。。 go create proc/view 。。。