您的位置:首页 > 数据库

重复执行SQL判断语句整合

2015-09-30 15:36 501 查看
判断视图是都存在

IF object_id(
'viewname'
IS
 
not
 
NULL


begin


 
--操作


 
--drop view viewname


end


GO


判断表是否存在

IF object_id(
'tablename'
IS
 
NULL


BEGIN


 
--操作


END


GO


判断列是否存在

IF 
NOT
 
EXISTS (
SELECT
 
FROM
 
dbo.syscolumns 
WHERE
 
[
name
]=
'columnname'
 
AND
 
id=object_id(
'tablename'
))


begin


 
--操作


end


go


 判断函数是都存在

IF exists (
select
 
from
 
sysobjects 
where
 
xtype=
'fn'
 
and
 
name
=
'funcname'
)


BEGIN


 
--drop function funcname


END


判断存储过程是否存在

IF exists (
select
 
from
 
sysobjects 
where
 
xtype=
'p'
 
and
 
name
=
'procname'
)


BEGIN


 
--drop proc procname


end


判断触发器是存在

IF exists (
select
 
from
 
sysobjects 
where
 
id=object_id(N
'tr_es_Order_upd'
and
 
objectproperty(id,N
'IsTrigger'
)=1) 


begin


DROP
 
TRIGGER
  
tr_es_Order_upd ;


end


go


 创建索引

IF 
NOT
 
EXISTS (
select
 
from
 
sys.indexes 
where
 
name
=
'index_cb_WarehouseInOutDtl_MaterialsGUID'
)


begin


CREATE
 
INDEX
 
index_cb_WarehouseInOutDtl_MaterialsGUID


ON
 
cb_WarehouseInOutDtl (MaterialsGUID)    


END


GO


 转载自:公司知识库,作者:吴东
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql 重复执行