日期:2014-05-18  浏览次数:20447 次

请教一个触发器问题
table1表中有两列date1和date2,分别表示合同交货期和内部交货期,想实现仅在新插入数据时date2=date1,其他条件下不触发,语句如下:
create Trigger trigg-name
On table1  
for insert  
As  
begin
if exists(select 1 from inserted) and not exists(select 1 from deleted)
Update a 
Set a.date2 =b.date1 
From table1 a,inserted b
Where a.finterid=b.finterid 
end

执行的结果是:不管是insert还是update,触发器均被触发,请问怎么修改?

------解决方案--------------------
SQL code

create Trigger trigg-name
On table1  
for insert  
As  
if exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
Update a 
Set a.date2 =b.date1 
From table1 a,inserted b
Where a.finterid=b.finterid 
end
--你的begin end位置放得有问题。应该是先判断什么操作,然执行对应的操作

------解决方案--------------------
SQL code

create Trigger trigg-name
On table1   
for insert   
As   
begin
Update a  
Set a.date2 =b.date1  
From table1 a,inserted b
Where a.finterid=b.finterid  
end