Solved

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

Posted on 2014-07-17
5
78 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
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

726 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