我想在 SQL Server 中编写一个触发器,以便在 table1
中插入一行时将 2 行插入 table2
。我想使用 table1
中的列值。
所以我的触发器看起来像这样
create trigger triggername on table1
as
begin
insert into
insert into
end
如何从最后插入的行(触发触发器的行插入)中获取任何列的值。 IE。相当于 oracle 中的“引用行”
最佳答案
SQL Server 中的触发器按语句触发,而不是按行触发。您可以使用两个伪表 inserted
和 deleted
(对于 insert
触发器,唯一感兴趣的是 inserted
)
CREATE TRIGGER YourTrigger ON Table1
FOR INSERT
AS
INSERT INTO Table2
SELECT * from inserted /*This will contain multiple rows if it is a multi-row insert*/
关于sql server 触发器引用最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4323911/