SQL SERVER,如何将记录被修改或添加的的服务器时间同时送到这条记录的时间字段里?/(触发器)

yhec 2009-05-14 06:05:58
SQL SERVER,如何将记录被修改或添加的的服务器时间同时送到这条记录的时间字段里?/(触发器)
----------------------------------------------------------------
估计要用触发器了。APPEND(INSERT)或UPDATE记录时,把当前时间也自动记录下来,送到这条记录的“更新时间”字段里。
...全文
121 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
BCBPLC 2009-05-15
  • 打赏
  • 举报
回复
create trigger SetUpdateTime
on table1
for insert,update
as
begin
set nocount off
update table1 set UpdateTime = getdate()
from table_name a,inserted b
where a.ID = inserted.ID
set nocount on
end

这样肯定可以的,跟1楼一样
yangangs 2009-05-15
  • 打赏
  • 举报
回复
楼上说的对吧
BCBPLC 2009-05-15
  • 打赏
  • 举报
回复
对,如果只想要记录产生的时间,就加:b.UpdateTime is null
否则:where a.ID = b.ID 就可以了
僵哥 2009-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 BCBPLC 的回复:]
create trigger SetUpdateTime
on table1
for insert,update
as
update table1 set UpdateTime = getdate()
from table_name a,inserted b
where a.ID = inserted.ID and b.UpdateTime is null

[/Quote]
insert的时候就有更新updatetime,那update的时候还会是null?
BCBPLC 2009-05-15
  • 打赏
  • 举报
回复
create trigger SetUpdateTime
on table1
for insert,update
as
update table1 set UpdateTime = getdate()
from table_name a,inserted b
where a.ID = inserted.ID and b.UpdateTime is null
僵哥 2009-05-14
  • 打赏
  • 举报
回复
create trigger trigger_name
on table_name
for insert,update
as
begin
SET NOCOUNT ON;
update table_name set update_time = getdate()
from table_name,inserted
where table_name.relation_field = inserted.relation_field
SET NOCOUNT OFF;
end

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧