oracle job 一直是scheduled,How to use variable in a Oracle scheduled job?

The following script ran in Oracle SQL developer. It will insert two rows into a table. However, when I put this script into a job under the scheduler tab in Oracle SQL developer, no row was inserted. When created the job, I used sys.Default_job_class, type of job is PL/SQL block.

Can someone help me to revise the script, so it can be run in a job? Thank you

define batchNo='123';

insert into TABLE_NAME (col1, col2, col3, col4) select col1, col2, col3, col4 from TABLE2 WHERE BATCH_NO = '&batchNo';

commit;

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Change your code so it's a PL/SQL block:

DECLARE

batchNo VARCHAR2(3) := '123';

BEGIN

insert into TABLE_NAME (col1, col2, col3, col4)

select col1, col2, col3, col4

from TABLE2

WHERE BATCH_NO = batchNo;

commit;

END;

define and &varname are used in SQL*Plus, but are not valid PL/SQL.

# Answer 2

Your syntax with "&batchNo" is specific to SQL*Plus. It will not work with PL/SQL.

PL/SQL would need something like this:

DECLARE

l_batchno number := 123;

BEGIN

insert into TABLE_NAME (col1, col2, col3, col4)

select col1, col2, col3, col4 from TABLE2 WHERE BATCH_NO = l_batchno;

commit;

END;

# Answer 3

If you want to execute with parameter you must do:

sqlplus -s $USER/$PASSWD @ script.sql

Your SQL Script must contain:

INSERT INTO TABLE_NAME (col1, col2, col3, col4)

SELECT col1, col2, col3, col4 FROM TABLE2 WHERE BATCH_NO = '&1';

COMMIT;

If you want to use more than 1 parameter...

sqlplus -s $USER/$PASSWD @ script.sql ...

You must put into script.sql

DECLARE

v_FIRST DATATYPE;

v_SECOND DATATYPE;

v_N DATATYPE;

BEGIN

v_FIRST := '&1';

v_SECOND := '&2';

v_N := '&N';

END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值