Solved

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

Posted on 2014-07-17
5
57 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:ScottPletcher
ScottPletcher earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

12 Experts available now in Live!

Get 1:1 Help Now