| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1810 人关注过本帖
标题:多个触发器怎么设置
只看楼主 加入收藏
jinzhengbe
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2012-3-30
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
多个触发器怎么设置
alter trigger oknumcgxd --第1次运行时,create,
on [dbo].[newmoo_case_cgxd]
for insert
as
begin
  declare @caseid bigint
  declare @ischeck int
  select @caseid = caseid from inserted
  select @ischeck = ischeck from inserted

  if exists(select id from newmoo_case where id=@caseid and ischeck >='5')
     begin     
       update a set a.oknum=b.productnum,end_date=getdate()
       from newmoo_case a,(select caseid,productnum=sum(productnum) from newmoo_case_cgxd where ischeck >='5' group by caseid ) b
       where ischeck >='5'and a.id=b.caseid
     end
  else
     begin
       insert into newmoo_case(id,oknum,end_date)
       select caseid,productnum,getdate() from inserted
     end


  if exists(select id from newmoo_case where id=@caseid and ischeck ='3')
     begin     
       update a set a.oknum=b.productnum,end_date=getdate()
       from newmoo_case a,(select caseid,productnum=sum(productnum) from newmoo_case_cgxd where ischeck ='3' group by caseid ) b
       where ischeck ='3'and a.id=b.caseid
     end
  else
     begin
       insert into newmoo_case(id,oknum,end_date)
       select caseid,productnum,getdate() from inserted
     end

 end

代码如上 我设置了两个触发器,如果单独写一个,触发器可以正常工作,
但是如果 像上面 把两个触发器写在一起,或者分别做成两个独立的触发器,
都会不工作,请问是什么原因。应该怎么解决,

非常感谢
2017-04-02 20:49
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:191
帖 子:3146
专家分:8408
注 册:2015-3-25
收藏
得分:10 
看样子,好像是 区别在这里 ischeck ='3' 或者ischeck ='5'
你就把2个分支合并
if exists(select id from newmoo_case where id=@caseid and ischeck in('3','5'))
     begin     
       update a set a.oknum=b.productnum,end_date=getdate()
       from newmoo_case a,(select caseid,productnum=sum(productnum) from newmoo_case_cgxd where ischeck  in('3','5') group by caseid ) b
       where ischeck  in('3','5') and a.id=b.caseid
     end
  else
     begin
       insert into newmoo_case(id,oknum,end_date)
       select caseid,productnum,getdate() from inserted
     end

2017-04-03 00:06
jinzhengbe
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2012-3-30
收藏
得分:0 
不是的。 有两个区别

ischeck =3的时候 更新的 overnum字段
ischeck=5的时候 更新的是 oknum字段


还有个问题就是 ,执行上面的代码后,会对整个表重新计算,
这样的话是不是服务器压力会很大,

如果想只想更新,被插入的行 或者更新的话,
有什么好的建议么?

2017-04-03 13:05
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:191
帖 子:3146
专家分:8408
注 册:2015-3-25
收藏
得分:0 
以下是引用jinzhengbe在2017-4-3 13:05:19的发言:

不是的。 有两个区别
 
ischeck =3的时候 更新的 overnum字段
ischeck=5的时候 更新的是 oknum字段
 
 
还有个问题就是 ,执行上面的代码后,会对整个表重新计算,
这样的话是不是服务器压力会很大,
 
如果想只想更新,被插入的行 或者更新的话,
有什么好的建议么?
 
alter trigger oknumcgxd --第1次运行时,create
on [dbo].[newmoo_case_cgxd]
for insert
as
begin
  declare @caseid bigint,@ischeck int

  select @caseid = caseid,@ischeck = ischeck from inserted

  if exists(select id from newmoo_case where id=@caseid and ischeck in('3','5'))
     begin

       if @ischeck='5     -- ischeck=5的时候 更新的是 oknum 字段
          begin
            update a set a.oknum=b.productnum,end_date=getdate()
            from newmoo_case a,(select caseid,productnum=sum(productnum) from newmoo_case_cgxd where ischeck >='5' group by caseid ) b
            where ischeck >='5'and a.id=b.caseid
          end
       else               -- ischeck =3的时候 更新的 overnum 字段,但你更新语句不对
          begin
            update a set a.oknum=b.productnum,end_date=getdate()
            from newmoo_case a,(select caseid,productnum=sum(productnum) from newmoo_case_cgxd where ischeck ='3' group by caseid ) b
            where ischeck ='3'and a.id=b.caseid
          end
     end
  else
     begin
       insert into newmoo_case(id,oknum,end_date)
       select caseid,productnum,getdate() from inserted
     end

 end
2017-04-03 16:21
快速回复:多个触发器怎么设置
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015260 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved