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