您的位置:首页 > 数据库

SQL语句判断指定的数据库、表、字段、存储过程是否存在

2010-09-27 17:38 771 查看
直接上代码,对大家有用就好!

代码

--判断[TestDB]是否存在
if exists(select 1 from master..sysdatabases where name='TestDB')
print 'TestDB存在'
else
print 'TestDB不存在'

--判断表[TestTb]是否存在
if exists(select * from TestDB..syscolumns where id=object_id('TestDB.dbo.TestTb'))
print '表TestTb存在'
else
print '表TestTb不存在'

--判断[TestDB]数据中[TestTb]表中是否存在[Name]字段
if exists(select * from TestDB..syscolumns where id=object_id('TestDB.dbo.TestTb') and name='Name')
print '字段Name存在'
else
print '字段Name不存在'

If exists(select name from sysobjects where name='存储过程名' and type='p')
drop proc 存储过程名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: