.sql脚本中的SQL加循环

时间:2017-08-03 12:07:10

标签: oracle sqlplus

我是编写SQL加脚本的新手,我有一个问题。我想为我的PL / SQL实用程序编写安装脚本,该实用程序向用户询问一些问题(通过ACCEPT命令),然后列出将要执行的操作(通过PROMPT)并要求确认(再次通过ACCEPT)。这部分没问题,但是我遇到问题,如果他拒绝接受或者再问他所有问题,我需要运行一些PL / SQL脚本。是在SQL加上任何选项如何做一个交互式循环,根据变量内容决定是继续运行PL / SQL脚本还是循环回循环开始?

伪代码中的

我想要这样的东西:

 while ('&confirmation' != 'y'){
    /*ask some questions*/
    ACCEPT info_1 CHAR PROMPT 'text_1';
    ACCEPT info_2 CHAR PROMPT 'text_2';
    ...
    /*write some summary*/
    PROMPT 'text_2';
    /*ask for confirmation */
    ACCEPT confirmation CHAR PROMPT 'are these information correct? (y/n)';
 }
 @skript

由于

1 个答案:

答案 0 :(得分:2)

<强> input.sql

accept username char prompt "username: "
accept password char prompt "password: "

prompt
prompt &username
prompt &password
prompt
prompt

accept confirm char prompt "correct? (y|n): "

column col noprint new_value runscript
select
  decode(lower('&confirm'),
    'y', 'task.sql',
    'n', 'input.sql') col
from dual;

prompt &runscript
@&runscript

<强> task.sql

prompt run my tasks with &username and &password in place

<强>测试

me@XE> @input
username: a
password: b

a
b

correct? (y|n): y
old   2:   decode(lower('&confirm'),
new   2:   decode(lower('y'),

task.sql
run my tasks with a and b in place
me@XE> @input
username: a
password: b

a
b


correct? (y|n): n
old   2:   decode(lower('&confirm'),
new   2:   decode(lower('n'),

input.sql
username: etc...