我一个T表,里面有字段A,字段B,字段C触发器要求,当字段A数字小于0,即当字段A变为负数时自动回滚字段A和字段B另:当字段C大于12万时,自动更新为12万!请问怎么样实现这个功能??

解决方案 »

  1.   

    create trigger tr_T on T
    for insert,update
    as
    if exists(select 1 from inserted where C>120000)
    update 表
    set C=1200000
    from inserted i
    where 表.主键=i.主键 and i.C>120000
    当字段A数字小于0,即当字段A变为负数时自动回滚字段A和字段B
    --------------------------
    这句话没看懂
      

  2.   

    当字段A数字小于0,即当字段A变为负数时自动回滚字段A和字段B
    --------------------------
    这句话没看懂=================================
    比如现在A字段的值现在为20,由于某些原因使A字段的值变为了-1,这时能不能用触发器将A字段的值重新变为20,即是回滚.
      

  3.   

    create trigger tr_T on T
    for insert,update
    as
    if exists(select 1 from inserted where C>120000)
    update 表
    set C=1200000
    from inserted i
    where 表.主键=i.主键 and i.C>120000if exists(select 1 from inserted where A<0) and exists(select 1 from deleted)
    update 表
    set A=d.A
    from inserted i,deleted d
    where 表.主键=i.主键 and i.主键=d.主键
    and i.A<0 and exists(select 1 from deleted)
      

  4.   

    select 1
    这个1是不是自己的表名??inserted i
    这个i指的是那个表?系统临时生成的表??
      

  5.   

    select 1祇是為了判斷是否存在記錄,在exists查詢中通常會這樣用,在這裏和select *的效果是一樣的