0
update IQAlarm
set IQAD=GETDATE(),AD=AD+1
where exists (select CPC from Inquiry where Inquiry.IQST='ooo' 
  and DATEDIFF(DAY,GETDATE(),Inquiry.QDL)<=3 and Inquiry.CPC = IQAlarm.CPC);

insert into IQAlarm (CPC)  
select CPC from Inquiry   
where not exists (select CPC from Inquiry where Inquiry.IQST='ooo' 
and DATEDIFF(DAY,GETDATE(),Inquiry.QDL)<=3 and Inquiry.CPC = **IQAlarm.CPC**);

更新工作正常,但插入会产生此错误:

无法绑定多部分标识符“IQAlarm.CPC”。

4

1 回答 1

1

您的第一个更新应该有一个FROM支持相关性的子句(但我并不感到震惊,它没有一个也可以工作 - 这不是我写它的方式)。

UPDATE IQAlarm
set IQAD=GETDATE(),AD=AD+1
FROM IQAlarm -- <----- you need this
where exists (select CPC from Inquiry 
  where Inquiry.IQST='ooo' and DATEDIFF(DAY,GETDATE(),Inquiry.QDL)<=3 
  and Inquiry.CPC = IQAlarm.CPC);

我认为你的第二个都搞砸了。也许这就是你的意思:

insert into IQAlarm (CPC)
  select CPC from Inquiry
  where IQST='ooo' 
   and DATEDIFF(DAY,GETDATE(),Inquiry.QDL)<=3 
  and not exists (SELECT 1 FROM IQAlarm WHERE CPC = Inquiry.CPC);
于 2013-08-14T18:11:11.203 回答