|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Yesterday @ 2:07 AM
Points: 11,
Visits: 66
|
|
Hello All,
I hope someone can help because I'm lost as to what the problem is...
I have created a trigger:
CREATE TRIGGER new_contact ON contacts FOR INSERT AS declare @contact_id varchar(11) declare @body varchar(2000) declare @staff_issued varchar(10) SELECT @contact_id = contact_id, @staff_issued = staff_issued FROM 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 and then I run:
INSERT INTO contacts (contact_id, date_issued, staff_issued) VALUES ('ABC12345678', GETDATE(), 'ADMIN') 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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 8:39 AM
Points: 1,609,
Visits: 1,103
|
|
Try this bit in the middle:
SELECT @contact_id = contact_id, @staff_issued = staff_issued FROM inserted
The inserted "table" contains the record you have inserted whilst contacts won't know which record you want.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 11:13 AM
Points: 6,351,
Visits: 5,369
|
|
Also beware the the inserted table can contain more than one row if a multiple row insert is done.
Far away is close at hand in the images of elsewhere. Anon.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Yesterday @ 2:07 AM
Points: 11,
Visits: 66
|
|
Awesome that works.
Many Thanks
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:34 PM
Points: 32,930,
Visits: 26,815
|
|
WiRL (2/27/2013) Awesome that works.
Many Thanks
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.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|