mssql sqlserver 触发器中update()关键字说明简介


摘要:
下文讲述触发器中update()关键字的用法,如下所示:
实验环境:sqlserver 2008 R2


update()功能说明

update()函数的功能是检测列值是否被修改

update()举例说明

 create table A(keyId int, info varchar(30))   
 go
 create table ALog(keyId int, info varchar(30),
                           sort nvarchar(30),
    updatetime datetime default getdate())
  go
---生成基础数据
 insert into A(keyId,info)values(1,N'sql博客'),(2,N'sql教程')
go

---创建update触发器和update函数应用
create trigger tr_A 
   on A
 after update 
 as 
   if (update(keyId) or update(info))  ---检测列是否改动
   begin
    insert into ALog(keyId,info,sort)
     select keyId,info,N'修改前' from deleted 
    insert into ALog(keyId,info,sort)
     select keyId,info,N'修改后' from inserted 
   end
go

update A set keyId=100,info='aa' 
  where keyId =1 
   go
select * from ALog
  go

update A set keyId=98,info='testInformation' 
  where keyId =100 
   go
select * from ALog
  go


   truncate table A
   drop       table A 
    go
   truncate table ALog
   drop       table ALog 
    go