SQL触发器

我有如下一段代码,运行得到的结果是这样的,有会的大佬帮我看看问题出在哪了嘛

alter  trigger ui_value

  on t_martarget2--触发器对象表

  for update ,insert--插入或更新时触发.

  as  

   begin  

  if EXISTS (

  select 1 from t_martarget2  A ,inserted B 

  where A.plants=B.plants and A.mattype=B.mattype and A.steps=B.steps )--检查插入或更新的数据在DIC_DATAupdate表中是否存在,有则更新,无则添加


  update t_martarget2 set enddate=GETDATE()  from inserted B

  where t_martarget2.plants=B.plants and t_martarget2.mattype=B.mattype and t_martarget2.steps=B.steps  

  --为记录数据插入或更新时间,增加时间(lasttime)字段.

  insert into t_martarget2 select [plants],[steps],[mattype],[tarvalue],getdate(),'2100-01-01' from inserted

  end 

企业微信截图_15754303428134.png

lxy2 发布于 2019-12-4 11:32
1min目标场景问卷 立即参与
回答问题
悬赏:4 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
萌萌的呆贼Lv4初级互助
发布于2019-12-4 13:01(编辑于 2019-12-4 13:03)

update的地方修改下:

begin  

  if EXISTS (

  select 1 from t_martarget2  A ,inserted B 

  where A.plants=B.plants and A.mattype=B.mattype and A.steps=B.steps )--检查插入或更新的数据在DIC_DATAupdate表中是否存在,有则更新,无则添加

begin

update a set enddate=GETDATE()  from t_martarget2 as a inner join inserted B

on a.plants=B.plants and a.mattype=B.mattype and a.steps=B.steps  

  end

  else

  begin

  --为记录数据插入或更新时间,增加时间(lasttime)字段.

  insert into t_martarget2 select [plants],[steps],[mattype],[tarvalue],getdate(),'2100-01-01' from inserted

end

  end 


  • 2关注人数
  • 468浏览人数
  • 最后回答于:2019-12-4 13:03
    请选择关闭问题的原因
    确定 取消
    返回顶部