表TB,如果删除ID记录为7.1的记录,如何用触发器实现回滚
 create table tb (id nvarchar(10),col2 nvarchar(100))
insert into tb select '7.1','2' union select '7.2','3'

解决方案 »

  1.   


    if object_id('tb') is not null
    drop table tb
    go
    create table tb (id nvarchar(10),col2 nvarchar(100))
    insert into tb select '7.1','2' 
    union select '7.2','3'create trigger nothan
    on tb for delete
    as
    begin
    if exists(select 1 from deleted where id='7.1')
    begin
    raiserror 50005 N'不许瞎删除!'
    rollback
    end
    enddelete tb where id='7.1'
    --消息 50005,级别 16,状态 1,过程 nothan,第 7 行
    --不许瞎删除!
    --消息 3609,级别 16,状态 1,第 1 行
    --事务在触发器中结束。批处理已中止
      

  2.   

    如楼上即可 :)
    create trigger nothan
    on tb for delete
    as
    begin
    if exists(select 1 from deleted where id='7.1')
    begin
    raiserror 50005 N'不许瞎删除!'
    rollback
    end
    end