Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Sqlserver Trigger a quick look

By , 22 Apr 2011
Rate this:
Please Sign up or sign in to vote.
This article demonstrates the #1 failure when people write triggers.
When writing SQL triggers you can NEVER be sure that you will ONLY have one record when processing the trigger results.
In Your example only the first record from the INSERTED table will be captured, or worse random bits through out will be logged as a single record.
 
Insert into Usertable ([name])
Values ('Bob'),
       ('Luke'),
       ('Frank'),
       ('James');
 
Your trigger would only capture Bob.
In order to fix this you need to write a cursor, or a CTE, or in this case your not actually performing any real work, you can do this.
INSERT INTO [TestDb].[dbo].[tt]
SELECT
 [ID],
 [Name],
 'Inserted Record -- After Insert'
FROM INSERTED
 
This actually simplifies your example as it doesn't require numerous variables to store values.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SalizarMarxx
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 22 Apr 2011
Article Copyright 2011 by SalizarMarxx
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid