首页 > 数据库技术 > 详细

SQL 常用判断语句

时间:2017-07-31 00:50:59      阅读:357      评论:0      收藏:0      [点我收藏+]

我们在做sql更新时,为防止sql重复执行报错,需要对所需要执行的对象进行判断是否存在;

常用判断脚本如下:

判断视图是否存在

IF object_id(viewname) IS not NULL
begin
 --操作
 --drop view viewname
end
判断表是否存在
IF object_id(tablename) IS NULL
BEGIN
 --操作
END
判断列是否存在
IF NOT EXISTS (SELECT 1 FROM dbo.syscolumns WHERE [name]=columnname AND id=object_id(tablename))
begin

 --操作

end
判断函数是否存在
 
IF exists (select 1 from sysobjects where xtype=fn and name=funcname)
BEGIN
 --drop function funcname
end
 
判断存储过程是否存在
IF exists (select 1 from sysobjects where xtype=p and name=procname)
BEGIN
 --drop proc procname
end
 
判断触发器是存在
IF exists (select * from sysobjects where id=object_id(Ntr_es_Order_upd) and objectproperty(id,NIsTrigger)=1) 
begin
--DROP TRIGGER  tr_es_Order_upd ;
end
判断索引是否存在 创建索引
IF NOT EXISTS (select 1 from sys.indexes where name=index_cb_WarehouseInOutDtl_MaterialsGUID)
begin
--操作
END

SQL 常用判断语句

原文:http://www.cnblogs.com/yx007/p/7260925.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!