Solved

Inserted vs Deleted Table?

Posted on 2013-06-13
2
286 Views
Last Modified: 2013-06-14
I'm looking to have a records sent to a history table. I would like to have a trigger that works when i delete or update any attribute within my table.
When i DELETE a record, should i be pulling that from the temp (Inserted or Deleted table).
Should these be seperate Triggers one for Update and one for Delete?
I'm just wanting to understand these correctly.
Will this work?
ALTER TRIGGER [dbo].[Changes_Segment] 
ON  [dbo].[Segment]
FOR UPDATE, Delete 
AS 

IF UPDATE(L_F_ADD) 
OR UPDATE(L_T_ADD) 
OR UPDATE(R_T_ADD)
OR UPDATE(R_F_ADD) 
OR UPDATE(ROADJUR)
    INSERT INTO dbo.Segment_History
    SELECT * FROM inserted

Open in new window

Thanks
0
Comment
Question by:PtboGiser
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39245816
for your requirement you have to pull it form the deleted table

simple thumb rule

inserted table always holds the new data (i.e. the data that will be there after update / insert)
Deleted table always holds the old data (i.e. the data that will be deleted / updated).

I hope this helps.

Thanks
surendra
0
 

Author Comment

by:PtboGiser
ID: 39247466
Pulling from 2 different tables should be done in 2 different Triggers correct?
As a general rule.
Thanks i was reading similar stuff online but really wanted to confirm it!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 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

20 Experts available now in Live!

Get 1:1 Help Now