视图是否存在:
INFORMATION_SCHEMA.VIEWS
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'ChangeBank_view')
DROP View ChangeBank_view
表是否存在:
if exists (select 1 from sysobjects where id = object_id('test.Owner.users'))
print '存在'
else
print '不存在'
Temp表否存在:
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#TempReCodeForNewLoginRoom'))
PRINT '存在'
ELSE
PRINT'不存在'
exists
select count(*) 查询结果必然存在,不管是否有。然后再用exists去判断必定为真if exists (select count(*) from sysobjects where id = object_id('test.Owner.users'))你这个查询结果必然为真,1或者0.外面用if exists的结果肯定是存在啊。改成select 1 from sysobjects where id = object_id('users'))
DECLARE @SNAME VARCHAR(20) DECLARE DD CURSOR FOR SELECT SNAME FROM S OPEN DD --楼上说的对.你要的是判断游标是否存在 select * from MASTER.dbo.syscursors where cursor_name='DD' --以下操作为判断游标是否存在的同时检测游标状态。 SELECT (CASE WHEN CURSOR_STATUS('global','DD')=1 THEN '游标的结果集至少有一行' WHEN CURSOR_STATUS('global','DD')=0 THEN '游标的结果集为空' WHEN CURSOR_STATUS('global','DD')=-1 THEN '游标被关闭' WHEN CURSOR_STATUS('global','DD')=-2 THEN '游标不适用' WHEN CURSOR_STATUS('global','DD')=-3 THEN '游标不存在' END) AS RESULT
1、判断是否存在addOneArticle这个存储过程 if Exists(select name from sysobjects where NAME = 'addOneArticle' and type='P') drop procedure addOneArticle 2、判断是否存在countAr这个触发器 if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[countAr]') and OBJECTPROPERTY(id, N'IsTrigger') = 1) drop trigger countAr 3、判断是否存在View_1这个视图 IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'View_1') DROP View View_1 4、判断是否存在USER_Fun这个用户函数 (注意此处的type 有两种,分别是'TF'-Table-valued Function 表值函数 或'FN'-Scalar-valued Function 标量值函数) if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[USER_Fun]') and (type = 'FN' or type = 'TF')) DROP FUNCTION USER_Fun 5、判断表'Tb'是否存在 if (exists (SELECT * FROM dbo.sysobjects where id = object_id(N'Tb')and OBJECTPROPERTY(id, N'IsUserTable') = 1)) DROP TABLE Tb 6、判断数据库是否存在 if exists( select * from master.dbo.sysdatabases where dbid=db_ID( 'scbjdb' ) ) drop database scbjdb else print 'no exist scbjdb'
临时表是否存在:
方法一:
use fireweb;
go
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..##TEMP_TBL'))
PRINT '存在'
ELSE
PRINT'不存在'
方法二:
use fireweb;
go
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#TEMP_TBL') and type='U')
PRINT '存在'
ELSE
PRINT'不存在'