我希望在SQL Server中编写一个触发器,以便在table2
中插入一行时向table1
插入2行。我想使用table1
中的列值。
所以我的触发器看起来像这样
create trigger triggername on table1
as
begin
insert into
insert into
end
如何从最后插入的行(触发触发器的行插入)中获取任何列的值。即相当于oracle中的“引用行”
答案 0 :(得分:5)
每个语句的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*/
答案 1 :(得分:0)
喜 我自己有解决方案。我错过了别名
从插入
中选择@ patient_no = fldL1BasCode应该是
从插入的i中选择@ patient_no = i.fldL1BasCode