日期:2014-05-18 浏览次数:20500 次
create table tb(nId int identity(1,1) primary key, colForUpdate int)
go
CREATE TRIGGER uTrigOntb ON  tb 
   AFTER UPDATE
AS 
BEGIN
    declare @nHasUpdateTo2 int
    select @nHasUpdateTo2=count(1) from inserted I join deleted D on I.nId=D.nId
    where I.colForUpdate=2 and D.colForUpdate<>2
    if @nHasUpdateTo2>0
    begin
        print '有' + rtrim(@nHasUpdateTo2) + '行数据的colForUpdate列被改为2'
        --do something else 
    end
END
GO
insert tb select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
update tb set colForUpdate=2
where colForUpdate>=5
drop trigger uTrigOntb
drop table tb
------解决方案--------------------
create table meixiaofeng(nId int identity(1,1) primary key, colForUpdate int)
go
CREATE TRIGGER uTrigOntb ON meixiaofeng  
  AFTER UPDATE
AS  
BEGIN
declare @nHasUpdateTo2 int
select @nHasUpdateTo2=count(1) from inserted I join deleted D on I.nId=D.nId
where I.colForUpdate=2 and D.colForUpdate < >2
if @nHasUpdateTo2 >0
begin
print  '有 ' + rtrim(@nHasUpdateTo2) +  '行数据的colForUpdate列被改为2 '
--do something else  
end
END
GO
insert meixiaofeng select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
update meixiaofeng set colForUpdate=2
where colForUpdate >=5