sql server 触发器

一个insert触发器

alter TRIGGER insert_pp
ON people
instead of INSERT
AS
BEGIN

declare @tname varchar(20)

set @tname=(select PeopleName from inserted)

if Exists(select * from People where PeopleName=@tname)
  begin

    print('sorry')

    rollback
  end
else
  insert into People select * from inserted    #通过触发器验收后,必须再插入一次

END
GO

 

 

 

ALTER TRIGGER [dbo].[update_pp]
ON [dbo].[People]
after update
AS
BEGIN

 

 

一个 update触发器


declare @tname varchar(20)

set @tname=(select PeopleName from inserted)

if Exists(select * from People where PeopleName=@tname
and PeopleID!=(select PeopleID from inserted) )      #     after触发器出发的时候,已经将数据更新到数据库里了,所以要将刚才的那个数据剔除掉。
begin
    print('sorry')
    rollback
end
                                                                                  #after触发器,则不需要再做一次update,因为after之前,数据已经更新完毕了

 

END

posted on 2019-11-17 14:41  耀扬  阅读(139)  评论(0编辑  收藏  举报

导航