可能重复:
SQL Server: how do I make a trigger that only affects the row that was updated/inserted?
Table User
Userid(number)
is_updated(char)
Table Version
Version_number(number)
userid(number)
现在,如果我在版本表中插入/更新值,我想使用triger更新is_updated列,但是如何获取更新/插入行的用户ID?
这是第一次有触发器,有人可以帮助我
答案 0 :(得分:0)
在发布此类问题之前,您应该在CREATE TRIGGER声明中检查在线图书的文档。触发器只不过是一个特殊的存储过程,它继承了两个特殊的虚拟表,插入和已删除。所以尝试这样的事情:
create trigger dbo.version_TR01
on dbo.version
for insert, update
as
begin
--
-- make sure everybody has a row in the user table
--
insert dbo.[user]( user_id , is_updated )
select i.user_id , 'F'
from inserted i
where not exists ( select *
from dbo.[user] u
where u.user_id = i.user_id
)
--
-- mark everybody as updated
--
update dbo.[user]
set is_updated = 'T'
from dbo.[user] u
join inserted i on i.user_id = u.user_id
end
应该这样做。