标签:
create trigger [shema_name . ] trg_name on { table | view } [ with encryption ] { for | after | instead of } { insert , update , delete } as sql_statement
insert触发器实例
create trigger test on al for insert as declare @id int,@uid int,@lid int,@result char select @id=id,@uid=uid,@lid=lid,@result=result from inserted if(@lid=4) begin update al set uid=99 where id=@id print ‘lid=4时自动修改用户id为99‘ end
update触发器实例
create trigger test_update on al for update as 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 ‘删除成功‘
标签:
原文地址:http://www.cnblogs.com/lovejunjuan/p/4735811.html