The example will give a brief explanation of what i’m talking about:
AFTER INSERT,UPDATE
IF (This.Active = 1)
BEGIN
UPDATE Table2 SET AllActive = 1 WHERE This.ID = Table2.ID
END
ELSE
BEGIN
UPDATE Table2 SET AllActive = 0 WHERE This.ID = Table2.ID
END
I’m using this because that’s what i’m looking to do.
Do Triggers have a this like javascript?
If not, What’s the next best thing?
One common misconception about triggers in SQL Server: they are NOT fired per row affected – but by statement.
And therefore, when you have a statement that inserts or updates several rows, your
Inserted(and/orDeleted) pseudo tables in the trigger will contain multiple rows. Do NOT assume you’re dealing with a single row! Such a trigger will fail sooner or laterSo in your case, you would probably want to write something like this:
This will update all rows in the
Table2table and set theAllActivecolumn to the value ofActive, for each row that’s contained in theInsertedtable (contains the newly inserted rows, and the new values for the updated rows)