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

Posted on 2014-07-17
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.

Question by:Saroj13
  • 2
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()))

Accepted Solution

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:

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

Author Comment

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.

Author Comment

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()))

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL server 2008 and after encryption method 32 44
sql server insert 12 29
convert null in sql server 12 31
Find SQL query used by application 3 17
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

813 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

11 Experts available now in Live!

Get 1:1 Help Now