开发者

sql server trigger referencing last row

开发者 https://www.devze.com 2023-01-28 00:01 出处:网络
I want to write a trigger in SQL Server to insert 2 rows into table2 when a row is inserted into table1. I want to use a column value from table1.

I want to write a trigger in SQL Server to insert 2 rows into table2 when a row is inserted into table1. I want to use a column value from table1.

So my trigger looks like this

开发者_StackOverflow
create trigger triggername on table1
as
begin
insert into
insert into
end

How do I get the value of any column from the last inserted row (the row insertion which fires the trigger). I.e. the equivalent of 'referencing row' in oracle


Triggers in SQL Server fire per statement not per row. There are two pseudo tables inserted and deleted that you can use (for an insert trigger the only one of interest is 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*/


hi i have the solution myself. i missed the alias

select @patient_no=fldL1BasCode from inserted

should be

select @patient_no=i.fldL1BasCode from inserted i

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号