SQL简单触发器
表A 有字段 id content
我想写个触发器,在后台往表A 插入数据的时候,检查content的内容是否包括 'ABC'
如果包括,则不让其插入A中。
这个触发器该锄禾实现?
[解决办法]
create trigger tr_name on Afor insertasbegin if exists(select 1 from inserted where content like '%A%') rollbackend
[解决办法]
if object_id('a') is not null drop table agocreate table a( id int, content varchar(10))goif object_id('tr_a_insert') is not null drop trigger tr_a_insertgocreate trigger tr_a_insert on afor insertas if exists(select 1 from inserted where content like '%abc%') rollbackgoinsert into a select 1,'123'insert into a select 2,'6abc6'select * from a/*id content----------- ----------1 123(1 行受影响)*/
[解决办法]
一起执行肯定是不行的,相当一个批处理,出错的地方就停止了
[解决办法]
create trigger test on afor insertas if exists(select 1 from inserted where content like '%abc%') rollback else insert into a(content) select @content go