Friday, February 10, 2023

PL/SQL Procesure to delete the data older than 60 days

Here is a sample PL/SQL procedure that will delete records from a table that are older than 60 days:

CREATE OR REPLACE PROCEDURE delete_old_records (table_name IN VARCHAR2)

AS

BEGIN

  EXECUTE IMMEDIATE 'DELETE FROM ' || table_name || ' WHERE creation_date < SYSDATE - 60';

END;


This procedure takes the name of the table as an input  and deletes all records from that table whose creation_date is more than 60 days old. The EXECUTE IMMEDIATE statement is used to dynamically generate and execute a DELETE statement.


To call this procedure, you can use the following syntax:

BEGIN

  delete_old_records('my_table');

END;



No comments:

Post a Comment

🔄 PostgreSQL Database Cloning via Backup and Restore

 When working with PostgreSQL, there are many situations where you need to  clone a database : Creating a dev or staging environment from pr...