数据库为:sql2005
表Test,内容如下:字段A为int,B,C为日期,D为int
A B C D
1 2007-03-31 12:12:12 NULL 0
2 2007-03-31 12:12:12 NULL 0求这样一个触发器
当更新C字段内容时,计算C与B的时间差,把值赋给D。如,以上两条记录,当我更新A=2那条记录的C字段后,触发计算C,B的时间差(时间差用datediff计算),然后把值赋给D字段。
谢谢
表Test,内容如下:字段A为int,B,C为日期,D为int
A B C D
1 2007-03-31 12:12:12 NULL 0
2 2007-03-31 12:12:12 NULL 0求这样一个触发器
当更新C字段内容时,计算C与B的时间差,把值赋给D。如,以上两条记录,当我更新A=2那条记录的C字段后,触发计算C,B的时间差(时间差用datediff计算),然后把值赋给D字段。
谢谢
解决方案 »
- SQL的一个简单问题:按期计算时需要每两个月的数据相加,详见如下:
- 请教这样一个sql语句该怎么写
- 请问各位老大:在MS SQL Server中能正确运行的Update语句,到My SQL中后要作何修改?
- 请教sql语句
- 分组后如何写返回与分组的相关其它字段。
- 在代码中拼接SQL命令与在存储过程中拼接的区别
- 好难的问题.想的我头都暴了.各位大虾救救我吧
- 挑战专家:哪个存储过程占用了太多内存?
- ora---SQLCODE=1403 在 sql--怎么表示
- exec sp_executesql带单引号的问题
- 用a服务器放网站程序,b服务服做sql服务数据库,测试的时候SQL Server 不存在或访问被拒绝
- 求一SQL语句的写法!解决马上给分!
for update
as
if update(c)
and exists(select 1 from inserted i,deleted d where i.a=d.a and isnull(i.c,getdate())<>isnull(d.c,getdate()))
begin
update test
set d=datediff(day,b,c)
from test,inserted i,deleted d
where test.a=i.a and i.a=d.a and isnull(i.c,getdate())<>isnull(d.c,getdate())
end
create trigger tr on T for update
as
if update(C)
update T set T.D=datediff(day, T.B, tmpB.C)
from inserted tmpB
where T.A=tmpB.A
(A int,
B DateTime,
C DateTime,
D int)
Insert Test Select 1, '2007-03-29 12:12:12', NULL, 0
Union All Select 2, '2007-03-30 12:12:12', NULL, 0
GO
Create Trigger TR_UpdateD On Test
For Update
As
Begin
If Update(C)
Update A Set D = DateDiff(dd, B.B, B.C) From Test A Inner Join Inserted B On A.A = B.A
End
GO
Update Test Set C = GetDate()Select * From Test
GO
Drop Table Test
--Result
/*
A B C D
1 2007-03-29 12:12:12.000 2007-03-31 15:25:04.397 2
2 2007-03-30 12:12:12.000 2007-03-31 15:25:04.397 1
*/