判断数据库是否存在
if db_id('数据库名') is not null
drop database 。。。
go
create 。。。
判断数据表是否存在
select count(*) from sysobjects where id = object_id('数据库名.dbo.表名')
if exists (select count(*) from sysobjects where id = object_id('数据库名.dbo.表名'))
print '存在'
else
print '不存在'
判断临时表是否存在 #temp临时表名
if object_id(N'tempdb..#temp',N'U') is not null
begin
print '存在'
end
else
print '不存在'
判断字段是否存在
if exists(select * from syscolumns where name='colname1' and id=object_id('数据库名.dbo.表名'))
print '存在'
else
print '不存在'
(代表表tablename1中存在colname1字段 )
判断存储过程或试图是否存在
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 。。。
本文介绍如何使用SQL语句来判断数据库、数据表、临时表、字段及存储过程或视图是否存在,通过简单的示例代码帮助读者理解每种检查方法的具体实现。
756

被折叠的 条评论
为什么被折叠?



