1
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.

6
  • 2
    SQL Server does not support row level triggers Commented Jun 22, 2015 at 10:38
  • 1
    If you really need them you must work with cursors: stackoverflow.com/questions/5805413/… Commented Jun 22, 2015 at 10:39
  • @a_horse_with_no_name: Dear using INSERTED table you can work row by row. Or I don't understand the question? Commented Jun 22, 2015 at 10:41
  • I'm voting to close this question as off-topic because this is a feature that's just not present in the product - you cannot "magically" make it appear... Commented Jun 22, 2015 at 10:47
  • 2
    Sounds more like an xy problem. Why do you need a row-by-row trigger? Just about anything you could want to do can be done in a set-based way with the table trigger. What are you trying to achieve, @otariki? Commented Jun 22, 2015 at 14:07

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.