首页 > 开发 > 综合 > 正文

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

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

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
 
endgoinsert test(f1,f2,f3) values(1,2,3)
go
select * from test
go
update test set f1=11 where fid=1
go
--问题:不能获得修改前的值???

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表