create trigger [shema_name . ] trg_nameon { table | view }[ with encryption ]{ for | after | instead of }{ insert , update , delete }assql_statement
insert触发器实例
create trigger teston alfor insertasdeclare @id int,@uid int,@lid int,@result charselect @id=id,@uid=uid,@lid=lid,@result=result from insertedif(@lid=4)begin update al set uid=99 where id=@id PRint 'lid=4时自动修改用户id为99'end
update触发器实例
create trigger test_updateon al for updateas declare @oldid int,@olduid int,@oldlid int,@newid int,@newuid int,@newlid int select @oldid=id,@olduid=uid,@oldlid=lid from deleted; select @newid=id,@newuid=uid,@newlid=lid from inserted if(@newlid>@oldlid) begin print 'newlid>oldid' rollback tran; end else print '修改成功'
delete触发器实例
create trigger test_delete on al for delete as declare @did int,@duid int,@dlid int select @did=id,@duid=uid,@dlid=lid from deleted if(exists(select * from list where @dlid=id)) begin print '无法删除' rollback tran; end else print '删除成功'
新闻热点
疑难解答