Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server "AFTER INSERT" trigger doesn't see the just-inserted row

Consider this trigger:

ALTER TRIGGER myTrigger     ON someTable     AFTER INSERT AS BEGIN   DELETE FROM someTable          WHERE ISNUMERIC(someField) = 1 END 

I've got a table, someTable, and I'm trying to prevent people from inserting bad records. For the purpose of this question, a bad record has a field "someField" that is all numeric.

Of course, the right way to do this is NOT with a trigger, but I don't control the source code... just the SQL database. So I can't really prevent the insertion of the bad row, but I can delete it right away, which is good enough for my needs.

The trigger works, with one problem... when it fires, it never seems to delete the just-inserted bad record... it deletes any OLD bad records, but it doesn't delete the just-inserted bad record. So there's often one bad record floating around that isn't deleted until somebody else comes along and does another INSERT.

Is this a problem in my understanding of triggers? Are newly-inserted rows not yet committed while the trigger is running?

like image 650
Joel Spolsky Avatar asked Jan 01 '09 18:01

Joel Spolsky


People also ask

How do I get the insert row in a trigger?

To create a trigger, we need to change the delimiter. Inserting the row into Table1 activates the trigger and inserts the records into Table2. To insert record in Table1. To check if the records are inserted in both tables or not.

What is after insert trigger?

An AFTER INSERT Trigger means that MySQL will fire this trigger after the INSERT operation is executed.

How do you check a trigger on a table?

To view database level triggers, Login to the server using SQL Server management studio and navigate to the database. Expand the database and navigate to Programmability -> Database Triggers. To view triggers at the server level, Login to Server using SSMS and navigate to Server Objects and then Triggers folder.

Why we use each row in trigger?

The FOR EACH ROW option determines whether the trigger is a row trigger or a statement trigger. If you specify FOR EACH ROW , then the trigger fires once for each row of the table that is affected by the triggering statement.


1 Answers

Triggers cannot modify the changed data (Inserted or Deleted) otherwise you could get infinite recursion as the changes invoked the trigger again. One option would be for the trigger to roll back the transaction.

Edit: The reason for this is that the standard for SQL is that inserted and deleted rows cannot be modified by the trigger. The underlying reason for is that the modifications could cause infinite recursion. In the general case, this evaluation could involve multiple triggers in a mutually recursive cascade. Having a system intelligently decide whether to allow such updates is computationally intractable, essentially a variation on the halting problem.

The accepted solution to this is not to permit the trigger to alter the changing data, although it can roll back the transaction.

create table Foo (        FooID int       ,SomeField varchar (10) ) go  create trigger FooInsert     on Foo after insert as     begin         delete inserted          where isnumeric (SomeField) = 1     end go   Msg 286, Level 16, State 1, Procedure FooInsert, Line 5 The logical tables INSERTED and DELETED cannot be updated. 

Something like this will roll back the transaction.

create table Foo (        FooID int       ,SomeField varchar (10) ) go  create trigger FooInsert     on Foo for insert as     if exists (        select 1          from inserted          where isnumeric (SomeField) = 1) begin               rollback transaction     end go  insert Foo values (1, '1')  Msg 3609, Level 16, State 1, Line 1 The transaction ended in the trigger. The batch has been aborted. 
like image 90
ConcernedOfTunbridgeWells Avatar answered Oct 04 '22 02:10

ConcernedOfTunbridgeWells