Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger on insert help


Trigger on insert help

Author
Message
WiRL
WiRL
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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
Richard Warr
Richard Warr
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2642 Visits: 1984
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.

_____________________________________________________________________
MCSA SQL Server 2012
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8281 Visits: 9566
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.


WiRL
WiRL
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 66
Awesome that works.

Many Thanks
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)

Group: General Forum Members
Points: 55097 Visits: 40401
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search