这是触发器,其中re字段 是ntext类型,在写入数据时,如果  re字段中含有"karlpaydayloans.com" 字符串,则输入数据中止,事务回退,但re字段不包括 "karlpaydayloans.com" 字符串时,执行插入成功,但数据没有写入数据库,请高手看一下create trigger [tr_table_insertupdate] on tableName
INSTEAD OF  insert,update  
as 
 if exists ( select 1 from inserted  where re like '%karlpaydayloans.com%' )  
begin  
RAISERROR ('不能修改或者添加',16,1);  
ROLLBACK TRANSACTION  
end 
insert数据库触发器

解决方案 »

  1.   

    instead 的意思就是替换掉,触发器里面你不显示插入,就不会再插入数据了。所以这里应该加一个else处理插入。
      

  2.   

    1L说的对加上else处理else if exists(select 1 from deleted) begin
    update tb set col1=a.col1,col2=a.col2... from deleted a where tb.id=a.id
    end
    else begin
    insert into tb select * from inserted
    end
      

  3.   

    换成after 触发器即可,你这个需求不应该使用instead of 触发器
      

  4.   

    试着分析下楼主你的逻辑:关于insert,录入的那部分是不包含你所指字符串的数据;
    关于update,更新的那部分也是不包含所指字符串的数据;那么,首先区分下执行的操作是insert还是updateif not exists (select 1 from deleted)
    begin
    insert into 表(所有字段)
    select 所有字段
    from inserted
    where charindex('karlpaydayloans.com',re) = 0
    end
    else
    begin
    update a
    set a.col = b.col,....
    from 表 a join inserted b on a.主键列 = b.主键列
    where charindex('karlpaydayloans.com',b.re) = 0
    end关于提示的部分,个人建议可以做一个日志表将提示的东西存进去,或者不用提示,做好数据的规整就可以。