首页 > 开发 > 综合 > 正文

表中某列被修改后触发器SQL例子

2024-07-21 02:06:34
字体:
来源:转载
供稿:网友


create table [test] (
 [fid] [int] identity (1, 1) not null ,
 [f1] [int] null ,
 [f2] [int] null ,
 [f3] [int] null ,
 constraint [pk_test] primary key  clustered
 (
  [fid]
 )  on [primary]
) on [primary]
go


alter trigger updatetest on [dbo].[test]
for insert, update, delete
as
begin
 declare @f1 int,
  @fid int,
  @oldf1 int
 if update(f1)
 begin
  select @oldf1=f1 from test where fid in (select fid from inserted)
  select @fid=fid,@f1=f1 from inserted
  print 'fid = ' + convert(varchar(10),@fid)
  print 'oldf1 = ' + convert(varchar(10),@oldf1)
  print 'f1 = ' + convert(varchar(10),@f1)
 end
 
end

go

insert test(f1,f2,f3) values(1,2,3)
go
select * from test
go
update test set f1=11 where fid=1
go
--问题:不能获得修改前的值???
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表