﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 General Discussion  / Trigger on insert help / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 12:54:21 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Trigger on insert help</title><link>http://www.sqlservercentral.com/Forums/Topic1424162-149-1.aspx</link><description>[quote][b]WiRL (2/27/2013)[/b][hr]Awesome that works.Many Thanks[/quote]Actually, it doesn't.  If you insert more than one rrow into the table that fires the trigger, the trigger will only process one row.  You need to rewrite the code in a set based fashion to get all of the rows.  Please see Books Online (press the {f1} key) and read up on the INSERTED and DELETED logical tables that are available when the trigger is doing its job.It's also a pretty bad idea to call email in a trigger.  If email is down, the trigger could fail which will rollback the external transaction.  It's a far bettter thing to insert what you want into a staging table and have a job sweep through that tablee to send the emails.</description><pubDate>Wed, 27 Feb 2013 07:15:00 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Trigger on insert help</title><link>http://www.sqlservercentral.com/Forums/Topic1424162-149-1.aspx</link><description>Awesome that works.Many Thanks</description><pubDate>Wed, 27 Feb 2013 04:52:07 GMT</pubDate><dc:creator>WiRL</dc:creator></item><item><title>RE: Trigger on insert help</title><link>http://www.sqlservercentral.com/Forums/Topic1424162-149-1.aspx</link><description>Also beware the the inserted table can contain more than one row if a multiple row insert is done.</description><pubDate>Tue, 26 Feb 2013 10:20:27 GMT</pubDate><dc:creator>David Burrows</dc:creator></item><item><title>RE: Trigger on insert help</title><link>http://www.sqlservercentral.com/Forums/Topic1424162-149-1.aspx</link><description>Try this bit in the middle:SELECT @contact_id = contact_id,@staff_issued = staff_issuedFROM insertedThe inserted "table" contains the record you have inserted whilst contacts won't know which record you want.</description><pubDate>Tue, 26 Feb 2013 10:16:53 GMT</pubDate><dc:creator>Richard Warr</dc:creator></item><item><title>Trigger on insert help</title><link>http://www.sqlservercentral.com/Forums/Topic1424162-149-1.aspx</link><description>Hello All,I hope someone can help because I'm lost as to what the problem is...I have created a trigger:[code="sql"]CREATE TRIGGER new_contactON contactsFOR INSERTASdeclare @contact_id varchar(11)declare @body varchar(2000) declare @staff_issued varchar(10) SELECT @contact_id = contact_id,@staff_issued = staff_issuedFROM contacts  SET @body = 'Customer with ID:' + @conatct_id + ' has been created by: '  + @staff_issued  EXEC msdb.dbo.sp_send_dbmail @recipients = 'myemail@email.com',@subject = 'New Customer',@body = @body GO[/code]and then I run:[code="sql"]INSERT INTO contacts                      (contact_id, date_issued, staff_issued)VALUES     ('ABC12345678', GETDATE(), 'ADMIN')[/code]The insert works, the triggers runs and emails me but it sends the top result from the table "contacts" rather than the one that has just inserted???I am obviously missing something.Can anyone point me in the right direction??Many Thanks in advance</description><pubDate>Tue, 26 Feb 2013 10:03:12 GMT</pubDate><dc:creator>WiRL</dc:creator></item></channel></rss>