当前位置: 代码迷 >> Sql Server >> 在一个表内自动更新的有关问题
  详细解决方案

在一个表内自动更新的有关问题

热度:38   发布时间:2016-04-27 13:14:09.0
在一个表内自动更新的问题
+++++++++++++++table++++++++++++++++++
ID NAME LEVEL LIMIT
1 Mike B 1000
2 Jack A 3000
3 Lucy C 500



这假设是一个银行卡的限定额度,Level A代表限定透支3000,B 1000,C 500。
执行update table set limit = 1000 where id = '3';
Lucy的limit就改为了1000,如何让表自动的更改level的value自动提升为B呢。
是在update的时候加入条件还是在create table的时候加入触发?

Thanks

------解决方案--------------------
SQL code
CREATE TRIGGER T_table ON dbo.table_tFOR UPDATEASif update(limit)begin    update table_t    set LEVEL = case when limit >= 3000 then 'A' WHEN limit >=1000 then 'B' ELSE 'A' end    from inserted i,deleted d    where i.id = d.id    and i.limit  <> d.limit     and i.id = table_t.idend