Solved

Inserted vs Deleted Table?

Posted on 2013-06-13
2
288 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 38
2016 SQL Licensing 7 41
SQL Query Help Top 1 and Distinct? 6 27
Need help with a Stored Proc on Sql Server 2012 4 11
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

831 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