sqlserver触发器范例
–建立表:
CREATE TABLE [dbo].[TEST] (
[FID] [int] IDENTITY (1, 1) NOT NULL ,
[F1] [datetime] NULL ,
[F2] [int] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TEST1] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[F1] [datetime] NULL
) ON [PRIMARY]
GO
–写入测试数据:
insert test(f1,f2) values(getdate(),3)
GO
select * from test
GO
insert test1(f1) values(getdate())
GO
select * from test1
GO
–建立触发器:
CREATE TRIGGER updatetest ON dbo.TEST
FOR UPDATE
AS
begin
declare @F1 datetime,
@FID int
if update(F1)
begin
select @FID=FID,@F1=F1 from INSERTED
update test1 set f1=@F1 where id =@FiD
PRINT ‘FID = ‘ + convert(varchar(10),@FID)
PRINT ‘F1 = ‘ + convert(varchar(10),@F1)
end
end
GO
–测试触发器:
update test set f1=getdate() where fid=1
GO
欢迎转载,请注明出处:亲亲宝宝