首页 > 数据库 > sqlserver触发器范例

sqlserver触发器范例

2007年3月20日 发表评论 阅读评论

–建立表:

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

分类: 数据库 标签: 2,175 次阅读
原文链接:http://www.wenhq.com/article/view_5.html
欢迎转载,请注明出处:亲亲宝宝
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.