Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger on insert help Expand / Collapse
Author
Message
Posted Tuesday, February 26, 2013 10:03 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 24, 2013 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
Post #1424162
Posted Tuesday, February 26, 2013 10:16 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:11 AM
Points: 2,209, Visits: 1,767
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.
Post #1424170
Posted Tuesday, February 26, 2013 10:20 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:02 AM
Points: 7,013, Visits: 7,180
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.

Post #1424171
Posted Wednesday, February 27, 2013 4:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 24, 2013 2:07 AM
Points: 11, Visits: 66
Awesome that works.

Many Thanks
Post #1424467
Posted Wednesday, February 27, 2013 7:15 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:55 PM
Points: 35,618, Visits: 32,214
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1424525
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse