我们如何在PL SQL中在特定时间后自动删除表行

2021-12-24 00:00:00 oracle plsql

我有下面给出的相同列的拖车表:

I have Tow table with Identical Columns Given Below:

表 1 工资单:

表 2 TEMP_PAYROLLFILE:

Table-2 TEMP_PAYROLLFILE:

我编写了一个 PL SQL 函数,它从表 PAYROLLFILE 中插入一个重复的行到 TEMP_PAYROLLFILE 并从表 PAYROLLFILE 中删除原始行.所以简而言之,表 TEMP_PAYROLLFILE 为从表 PAYROLLFILE 中删除的数据保留备份.

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.

这里是PL SQL函数代码:

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; 

但是,我想做的不止这些,我想从表 TEMP_PAYROLLFILE 在特定时间段结束后自动删除备份行 特定行.我需要触发器吗.我是新手,以前从未做过此类工作.如果有人知道这种技术,那么我将非常感谢您的帮助.如果需要更多详细信息,请告诉我.谢谢

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

更新:

我写了这个作业来清理旧行:它会起作用吗?

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;

推荐答案

您需要的是定期清理工作. 使用 DBMS_SCHEDULER 您可以配置重复执行此操作的作业:

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;

删除超过 60 天的记录.

to delete records that are more than 60 days old.

这些是一些关于如何创建预定的工作.如下所示,job_action 部分接受 Pl/SQL 脚本,您可以在其中放置清理逻辑.

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;
/

相关文章