CREATE TRIGGER TRIGG_Gbill_up
on G_bill for update
as
if exists(select * from inserted where G_0<0)
  begin
  RAISERROR('The value of G_0 should be >=0',16,1)
  rollback transaction
  end
go

解决方案 »

  1.   

    select * from inserted ?更新时用 Inserted
      

  2.   

    --测试
    create table g_bill(g_0 int)  --创建示例表insert g_bill                --插入数据
    select 5update g_bill set g_0=-4     --把数据更新成负值--返回错误消息
    /*
    服务器: 消息 50000,级别 16,状态 1,过程 TRIGG_Gbill_up,行 6
    The value of G_0 should be >=0
    */
    --表没有被更新,错误发生,更改被回滚.
    /*
    g_0         
    ----------- 
    5(所影响的行数为 1 行)
    */
      

  3.   

    --update触发器触发的时候有有两个在内存中的临时表.deleted表和inserted表.deleted表保存更新前的数据,inserted表保存更新后的数据.