user5005768Himadree
user5005768Himadree

Reputation: 1427

How Can We Delete A Table Row Automatically After a Specific Time in PL SQL

I have Tow table with Identical Columns Given Below:

Table-1 PAYROLLFILE:

enter image description here

Table-2 TEMP_PAYROLLFILE:

enter image description here

I have written a PL SQL Function, which inserts a duplicate row from Table PAYROLLFILE to TEMP_PAYROLLFILE and remove the original row from Table PAYROLLFILE.So in short, Table TEMP_PAYROLLFILE is keeping a backup for deleted data from Table PAYROLLFILE.

Here is the PL SQL Function Code:

FUNCTION Remove_transaction_by_id(employee_id NUMBER)
RETURN CLOB
AS
  cnt INT;
BEGIN
    INSERT INTO temp_payrollfile
    SELECT *
    FROM   payrollfile
    WHERE  empid = employee_id;

    DELETE FROM payrollfile
    WHERE  empid = employee_id;

    SELECT Count(*)
    INTO   cnt
    FROM   payrollfile;

    COMMIT;

    RETURN '<result><status>success</status> <row>'
           ||cnt
           || '</row></result>';
EXCEPTION
  WHEN OTHERS THEN
             RETURN '<result><status>Error</status></result>';
END remove_transaction_by_id; 

However, I want to do more than that, I want to Delete Backup rows Automatically from Table TEMP_PAYROLLFILE after a specific time period is over fort that specific row.Do i need triggers.I am new on that and never done this type of work before.If anyone know this kind of technique, then i would really appreciate your help.please let me know if further details required.Thanks

Updates:

I have write this job to cleanup old rows: will it work?

BEGIN
DBMS_SCHEDULER.CREATE_JOB (
   job_name             => 'oe.REMOVE_TEMP_PAYROLLFILE_JOB',
   job_type             => 'PLSQL_BLOCK',
   job_action           => 'BEGIN DBMS_STATS.GATHER_TABLE_STATS('oe',
                            'TEMP_PAYROLLFILE'); 
                            delete from TEMP_PAYROLLFILE where RECORDDATE < sysdate-1;
                            END;',
   start_date           => '20-JAN-16 1.00.00AM US/Pacific',
   repeat_interval      => 'FREQ=DAILY', 
   end_date             => '25-JAN-16 1.00.00AM US/Pacific',
   enabled              =>  TRUE,
   comments             => 'Gather table statistics');
END;

Upvotes: 4

Views: 15494

Answers (2)

Gergely Bacso
Gergely Bacso

Reputation: 14651

What you need here is a periodic cleanup job. Using DBMS_SCHEDULER you can configure a job that does this repeatedly:

delete from TEMP_PAYROLLFILE where recorddate < sysdate-60;

to delete records that are more than 60 days old.

These are some examples on how to create a scheduled job. As you see below, the job_action sections accepts a Pl/SQL script, in which you can place your cleanup logic.

BEGIN
DBMS_SCHEDULER.CREATE_JOB (
   job_name             => 'oe.my_job1',
   job_type             => 'PLSQL_BLOCK',
   job_action           => 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(''oe'',
                            ''sales''); END;',
   start_date           => '15-JUL-08 1.00.00AM US/Pacific',
   repeat_interval      => 'FREQ=DAILY', 
   end_date             => '15-SEP-08 1.00.00AM US/Pacific',
   enabled              =>  TRUE,
   comments             => 'Gather table statistics');
END;
/

Upvotes: 4

MT0
MT0

Reputation: 167962

Create a stored procedure to perform the deletion:

CREATE PROCEDURE remove_Temp_Transacts_by_ID(
  in_employee_id IN NUMBER,
  in_datetime    IN TIMESTAMP
)
AS
BEGIN
  DELETE FROM temp_payrollfile
  WHERE  empid     = in_employee_id
  AND    datetime <= in_datetime;
END;
/

Then in your function include a call to schedule a job to run the procedure at a later time:

DBMS_SCHEDULER.CREATE_JOB(
  JOB_NAME   => 'Remove_TTbID__JOB',
  JOB_TYPE   => 'STORED_PROCEDURE',
  JOB_ACTION => 'remove_Temp_Transacts_by_ID'
  START_DATE => ( SYSTIMESTAMP + INTERVAL '2' HOURS ),
  ENABLED    => FALSE,
  COMMENT    => 'One time job to remove temporary payroll file entries'
);

DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
  JOB_NAME   => 'Remove_TTbID__JOB',
  ARGUMENT_POSITION => 1,
  ARGUMENT_VALUE    => employee_id
);

DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
  JOB_NAME   => 'Remove_TTbID__JOB',
  ARGUMENT_POSITION => 2,
  ARGUMENT_VALUE    => SYSTIMESTAMP
);

DBMS_SCHEDULER.ENABLE( 'Remove_TTbID__JOB' );

Upvotes: 2

Related Questions