SQL 判断触发器不存在则执行创建触发器语句 怎么过不去 一直报错
ifexists(select*fromsysobjectswhereid=object_id(N'delpoints')andobjectproperty(id,N'I...
if exists (select * from sysobjects where id=object_id(N'delpoints') and objectproperty(id,N'IsTrigger')=2)
begin
go
CREATE TRIGGER delpoints ON GHXH_Customer AFTER UPDATE AS BEGIN update GHXH_Customer set points = 0 where name like '%零售顾客%' END
end 展开
begin
go
CREATE TRIGGER delpoints ON GHXH_Customer AFTER UPDATE AS BEGIN update GHXH_Customer set points = 0 where name like '%零售顾客%' END
end 展开
2个回答
展开全部
create trigger语句必须单独放在一个查询里执行才可以,前边不能有任何其他语句。你在create trigger之前放了go我想就是为了让它在单独的查询中执行,但是exists就不起作用了,而且go之前的begin和最后的end是在两个批查询中,本身就不能配对,语法就有问题。exists用法也不对,应该是not exists,objectproperty用法也错了。
如果楼主想实现判断触发器不存在则创建,那么,创建触发器的语句就得放在动态语句里来执行了
if not exists (select * from sysobjects where id=object_id(N'delpoints') and objectproperty(object_id('delpoints'),N'IsTrigger')=1)
begin
exec ('CREATE TRIGGER delpoints ON GHXH_Customer AFTER UPDATE AS BEGIN update GHXH_Customer set points = 0 where name like ''%零售顾客%'' END')
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询