现在的位置: 首页 > 综合 > 正文

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

2012年04月30日 ⁄ 综合 ⁄ 共 556字 ⁄ 字号 评论关闭

直接上代码,对大家有用就好!

代码

--判断[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 存储过程名
 
 

 

 

抱歉!评论已关闭.