Solved

How to create a log table of all deleted records Sql Server 2008?

Posted on 2014-07-17
5
63 Views
Last Modified: 2016-06-09
I am having SalesAccount Table and I have a job that calls the storedprocedure who deletes old records later than 3 years.  Table retains 3 year data. Jobs runs every day. I want to create a log table which stores all the records who are deleted successfully and the date last deleted.

SalesAccount Table:
      [ID] [int] IDENTITY(1,1) NOT NULL,
      [DateTimeStamp] [smalldatetime] NULL,
      [BankerInfo] [varchar](100) NULL,
      [Notes] [varchar](250) NULL

stored procedure:
delete from SalesAccount
where ID in
(
      select ID      from SalesAccount
      where DateTimeStamp <= (DATEADD(YEAR , -3 , GETDATE()))
)


Question: how to create a log table which contains the information:
id, details(comma delimited list of all records that are deleted), count of records that are deleted, loggedon(date of deletion). Insert this information in log table after job runs to delete data.

thanks
0
Comment
Question by:Saroj13
  • 2
5 Comments
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 40203062
Use the OUTPUT clause; for example:

delete from SalesAccount
output deleted.ID, deleted.DateTimeStamp, ... into SalesAccountLog ( ID, DateTimeStamp, ... )
 where ID in
 (
       select ID      from SalesAccount
       where DateTimeStamp <= (DATEADD(YEAR , -3 , GETDATE()))
 )
0
 
LVL 4

Accepted Solution

by:
Philip Portnoy earned 250 total points
ID: 40203072
What I'd do in your case is:

1) A trigger on your SalesAccount.
This trigger should be FOR DELETE.
It will copy all deleted rows into SalesAccount_log table (with the same structure + 1 additional column "Deleted date").
Trigger would look like:

CREATE TRIGGER logdeleted ON SalesAccount FOR DELETE AS
INSERT INTO SalesAccount_log SELECT [ID], [DateTimeStamp], [BankerInfo], [Notes], DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) FROM deleted

Open in new window


Then you can build a view on top of a SalesAccount_log table to present the data in the format you want
0
 

Author Comment

by:Saroj13
ID: 40204829
How to insert in the log table
All the ID's that are deleted from the salesaccount table in the comma delimited list and Count of how many items are deleted.
0
 

Author Comment

by:Saroj13
ID: 40205011
Would you please help me in
This will generate all the deleted rows in the Log Table. HELP--- I want Columns in the Log Table [ID(primary key), List of deleted IDS from SalesAccountTable in the comma delimited list, Count of deleted ids, Date of execution.
delete from SalesAccount
output deleted.ID, deleted.DateTimeStamp, ... into SalesAccountLog ( ID, DateTimeStamp, ... )
 where ID in
 (
       select ID      from SalesAccount
       where DateTimeStamp <= (DATEADD(YEAR , -3 , GETDATE()))
 )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

863 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

22 Experts available now in Live!

Get 1:1 Help Now