Solved

Delete Trigger Help

Posted on 2010-11-10
3
301 Views
Last Modified: 2012-05-10
I have very limited experience with triggers, but with the help of this site (I modified the answer to question 26082733) I was able to create a trigger that watches a table of email addresses for my customers and records when there is an Insert, Update, or Delete.  This seems to be working great for the inserts and updates, but the deletes don't have enough information to make them actually be useful.  In particular - our customer number is called the CUST_EDP_ID, when the delete occurs, the CUST_EDP_ID is not being captured by the trigger - it's coming out as NULL.  Even the EMAIL address that was deleted is not captured.  In a nutshell, how can I change this to capture the CUST_EDP_ID and EMAIL data for deletes?  This way we know which EMAIL was deleted.  The code and a screen cap sample of data are below.

Also, can you explain what the i.fieldnames means in this section of code (oh I bet this is important - lol)?
i.CUST_EDP_ID, i.EMAIL_ADDR_CD

Thanks!

----------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tg_CHANGES_EMAIL] on [dbo].[SV_MACORD_CUST_EMAIL]
FOR insert, update, delete
as

insert into dbo.Changes_EMAIL (Record_SLOTID, ChangeDate, MoveDate, CUST_EDP_ID, EMAIL_ADDR_CD, ChangeType)
select IsNull(i.SLOTID, d.SLOTID), getdate(), NULL, i.CUST_EDP_ID, i.EMAIL_ADDR_CD,
  case when i.SLOTID = d.SLOTID then 'U'
   when i.SLOTID is not null then 'I'
   else 'D' end
from inserted i
full outer join deleted d on i.SLOTID = d.SLOTID
-----------------------------------------------

 outputexample
0
Comment
Question by:BartWestphal
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
you have to use the ISNULL which you used on SLOTID also for the other columns
set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

GO

ALTER TRIGGER [dbo].[tg_CHANGES_EMAIL] on [dbo].[SV_MACORD_CUST_EMAIL]

FOR insert, update, delete

as



insert into dbo.Changes_EMAIL 

(Record_SLOTID, ChangeDate, MoveDate, CUST_EDP_ID, EMAIL_ADDR_CD, ChangeType)

select IsNull(i.SLOTID, d.SLOTID)

  , getdate(), NULL

  , isnull(i.CUST_EDP_ID, d.CUST_EDP_ID)

  , isnull(i.EMAIL_ADDR_CD, d.EMAIL_ADDR_CD)

  , case when i.SLOTID = d.SLOTID then 'U'

   when i.SLOTID is not null then 'I'

   else 'D' end

from inserted i

full outer join deleted d on i.SLOTID = d.SLOTID

Open in new window

0
 
LVL 14

Expert Comment

by:Emes
Comment Utility
case when i.SLOTID = d.SLOTID then 'U'
   when i.SLOTID is not null then 'I'
   else 'D' end
from inserted i


it is from the inserted/Updated record.

so it will have the new value that is in the table for that field.

I think it may be easier to make another trigger just for deletes and keep the update inserts in the original trigger.

0
 

Author Closing Comment

by:BartWestphal
Comment Utility
angellll, this solution worked perfectly!  Thank you for the quick, effective, and clear answer.  And I have some new knowledge too!
- Bart
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now