创建触发器

CREATE TRIGGER    [dbo].[table1_column1]  ON [dbo].table1
for INSERT, UPDATE
--after INSERT, UPDATE
AS
DECLARE @canshu1 varchar(30),
        @rq datetime,
        @rq2 datetime
SELECT @canshu1 = canshu1,
       @rq=rq
FROM  inserted
 set @rq2=(select rq2 from table2 where rq2=@canshu1)
 if(@rq>@rq2)
   update table2 set rq2=@rq where canshu1=@canshu1
 update table2 set updateTime=getdate() where canshu1=@canshu1

GO

原文地址:https://www.cnblogs.com/yubufan/p/4354727.html