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
开发者_StackOverflowcreate 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
精彩评论