?
Solved

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

Posted on 2014-07-17
5
Medium Priority
?
90 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 1000 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 1000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
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.
Suggested Courses

800 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