DBMS_JOB.SUBMIT 和 INSERT 语句

问题描述 投票:0回答:1

我正在尝试使用 INSERT 语句使用

DBMS_JOB.SUBMIT
来实现该过程,但我无法成功地将数据从一个表传输到另一个表。

DECLARE
    l_job NUMBER;
 BEGIN
    DBMS_JOB.SUBMIT(
       job       => l_job,
       what      => 'BEGIN insert into customers_tbl1 select * from customers_tbl; 
 END;'--,
     --  next_date => SYSDATE--, -- Schedule for 1 hour from now
       --interval  => 'SYSDATE'   -- Reschedule every day
    );
   COMMIT; -- Important to commit the transaction to schedule the job
 END;

表格数据:

    CREATE TABLE customers_tbl 
    ( customer_id number(10),
      customer_name varchar2(50),
      city varchar2(50)
    );
    insert into customers(customer_id,customer_name,city) values(1,'Albert','Munich');
    insert into customers(customer_id,customer_name,city) values(2,'Alex','Stuttgart');
    insert into customers(customer_id,customer_name,city) values(3,'Sasuke','Tokyo');
    
    CREATE TABLE customers_tbl1 
    ( customer_id number(10),
      customer_name varchar2(50),
      city varchar2(50)
    );
oracle19c
1个回答
0
投票

what 参数需要适当的 PL/SQL 块

(BEGIN ... END;)
并用分号终止
INSERT
语句。

更正代码:

DECLARE
    l_job NUMBER;
BEGIN
    DBMS_JOB.SUBMIT(
        job       => l_job,
        what      => 'BEGIN insert into customers_tbl1 select * from customers_tbl; END;',
        next_date => SYSDATE,       -- Schedule for immediate execution
        interval  => SYSDATE + 1    -- Repeats daily
    );
    COMMIT; -- Commit the transaction to schedule the job
END;
/

如果您想从现在开始安排一小时,可以在

next_date
中使用 SYSDATE + 1/24

验证数据是否传输:

SELECT * FROM customers_tbl1;

监控您的工作状态:

SELECT job, what, next_date, broken
FROM user_jobs;
© www.soinside.com 2019 - 2024. All rights reserved.