现在有表Essay,里面有数据E_SID,我需要保证这列数据的唯一性(除0外),因此在更新数据时,我用触发器检测,如果更改后的数据是A值,则将原来的E_SID=A的列都改为0,然后将更改列改为A。我的SQL如下:ALTER   trigger  [Trun_Zero] ON  [dbo].[Essay] 
instead of update as
if update(E_SID)
begin 
Update Essay set E_SID=0 from Essay
where E_SID in (select E_SID from Inserted)/*将其他置为0*/
Update Essay set Essay.E_SID=Inserted.E_SID from Inserted
where Essay.E_SID in (select E_SID from Inserted)/*将修改列置为需要修改的值*/
end但是现在不能实现,请问是哪里出问题了呢?

解决方案 »

  1.   

    好吧,我明白了。原来更新不了是因为下面更新部分定位错误,应该是:ALTER   trigger  [Trun_Zero] ON  [dbo].[Essay] 
    instead of update as
    if update(E_SID)
    begin 
    Update Essay set Essay.E_SID=0 from Inserted
    where Essay.E_SID=Inserted.E_SID
    Update Essay set Essay.E_SID=Inserted.E_SID from Inserted
    where Essay.E_ID=Inserted.E_ID
    end
    同时要注意:SQL中只显示出了当前更新列的变化,所以需要重新打开表才知道所有的变化