sql server 触发器

冷暖自知 提交于 2019-12-04 17:58:40

一个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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!