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

发表于:2007-07-02来源:作者:点击数: 标签:
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]


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
--问题:不能获得修改前的值???

原文转自:http://www.ltesting.net