Oto Shavadze
Oto Shavadze

Reputation: 42773

How to create trigger for each row in SQL Server

create table t1(col int);
create table t2(col int);


CREATE TRIGGER tr
ON t1
AFTER INSERT 
as
    begin
    INSERT INTO t2(col) values(1)
end;

-- tables and trigger created

INSERT INTO t1(col) values(1), (2)

When I run this insert query, in table t2 insert event happens just once.

Needed FOR EACH ROW statement in trigger, but how to make this in SQL Server? Not found in documentation, and after googling, not found some good solution also.

Upvotes: 1

Views: 12602

Answers (1)

Joe Taras
Joe Taras

Reputation: 15379

Replace your: INSERT INTO t2(col) values(1)

in:

INSERT INTO t2(col) SELECT col FROM inserted

Because in inserted table you'll find all rows inserted in table linked at your trigger (t1)

Upvotes: 3

Related Questions