SQL Stored procedure to delete multiple tables data from the database older than 1 years. Deletion should be performed in specified month after every 1 year
Loading
SQL Stored procedure to delete multiple tables data from the database older than 1 years. Deletion should be performed in specified month after every 1 year
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jayraj ChhayaPosted Dec 27, 2023, 7:43 AM
Please refer this one
Anandu G NathPosted Dec 27, 2023, 3:50 AM
You can use the above query as a refrence, according to your needs you can change.@Meghana M
Subarta RayPosted Dec 26, 2023, 12:57 PM
CREATE PROCEDURE DeleteOldData
AS
BEGIN
DECLARE @CurrentDate DATETIME = GETDATE();
DECLARE @RetentionDate DATETIME;
-- Set the retention date to the first day of January of the previous year
SET @RetentionDate = DATEADD(YEAR, DATEDIFF(YEAR, 0, @CurrentDate) - 1, 0);
-- Check if the current month is January before proceeding with deletion
IF MONTH(@CurrentDate) = 1
BEGIN
-- Delete data from Table1
DELETE FROM Table1 WHERE CreatedDate < @RetentionDate;
-- Delete data from Table2
DELETE FROM Table2 WHERE CreatedDate < @RetentionDate;
-- Add more tables as needed
PRINT 'Data older than 1 year deleted successfully.';
END
ELSE
BEGIN
PRINT 'Deletion is only performed in January.';
END
END;
Amit MohantyPosted Jun 5, 2023, 6:36 AM
To run a stored procedure in a specified month after every 1 year, you can use a combination of SQL Server Agent and a scheduled job.
Open SQL Server Management Studio and connect to your SQL Server instance.
Expand the "SQL Server Agent" folder in the Object Explorer.
Right-click on the "Jobs" folder and select "New Job". This will open the New Job dialog box.
In the "General" tab of the New Job dialog box, provide a name for the job in the "Name" field.
In the "Steps" tab, click on the "New" button to create a new step for the job. This will open the New Job Step dialog box.
In the New Job Step dialog box, provide a name for the step in the "Step name" field.
In the "Type" section, select "Transact-SQL script (T-SQL)" as the type.
In the "Database" field, select the appropriate database where your stored procedure exists.
In the "Command" field, enter the code to execute your stored procedure. Here's an example:
Click on the "OK" button to save the job step.
In the "Schedules" tab of the New Job dialog box, click on the "New" button to create a new schedule for the job. This will open the New Job Schedule dialog box.
In the New Job Schedule dialog box, configure the schedule as follows:
Click on the "OK" button to save the job schedule.
Review the job settings in the "Notifications", "Targets", and "History" tabs as per your requirements.
Click on the "OK" button to create the job.
Prasad RaveendranPosted Jun 2, 2023, 7:43 PM
The deletion process can be done in batches using a WHILE loop. Here's how it works:
By deleting data in batches, you can manage the impact on system resources and reduce the likelihood of long-running transactions or excessive log growth. Adjust the @BatchSize value based on performance testing and system capacity to find the optimal balance between deletion speed and resource usage.
Brahma Prakash ShuklaPosted Jun 2, 2023, 12:18 PM
Janarthanan SPosted Jun 2, 2023, 9:03 AM
CREATE PROCEDURE DeleteOldData
AS
BEGIN
-- Check if the current month is January
IF MONTH(GETDATE()) = 1
BEGIN
-- Delete data from the first table
DELETE FROM table1
WHERE date_column < DATEADD(YEAR, -1, GETDATE());
-- Delete data from the second table
DELETE FROM table2
WHERE date_column < DATEADD(YEAR, -1, GETDATE());
-- Delete data from the third table
DELETE FROM table3
WHERE date_column < DATEADD(YEAR, -1, GETDATE());
-- Continue deleting from other tables as needed
-- Add more DELETE statements for each table you want to delete data from
END
END
To execute
EXEC DeleteOldData;
Naimish MakwanaPosted Jun 2, 2023, 6:48 AM
Try below code:
Thanks