?
Solved

Delete Trigger Help

Posted on 2010-11-10
3
Medium Priority
?
312 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 34103295
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
ID: 34103314
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
ID: 34104081
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

840 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