首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么在某字段修改后给另一字段赋值

2012-03-31 
如何在某字段修改后给另一字段赋值请教:sql2000中如现在字段a值为b如何能让b改为c时给另一字段赋为当前时

如何在某字段修改后给另一字段赋值
请教:
sql2000中如现在字段a值为b
如何能让b改为c时给另一字段赋为当前时间

[解决办法]
1.你可以直接在update时赋予另一字段当前时间.
update tb set a = 'c' , 另一字段 = getdate() where a = 'b' 

2.也可以在触发器中赋予另一字段当前时间.
create trigger my_trig on tb for update
as
begin
update tb set 另一字段 = getdate() 
from tb , deleted d, inserted i
where tb.关键字 = d.关键字 and tb.关键字 = i.关键字 and d.a = 'b' and i.a = 'c'
end

[解决办法]

SQL code
--直接更新update tbl set a=c,另一字段=getdate() where a='b'--使用触发器create trigger tri_updatefor updateasbeginif exists(select 1 from detelet)and exists(select 1 from inserted)beginupdate tbl set 另一字段=getdate() from tbl adeleted b, inserted c where a.关键字段= b.关键字段and a.关键字段= c.关键字段and a.a = 'b' and c.a = 'c'endend 

热点排行