PL/SQL MENU在遵循LINK中的代码后表示怀疑



我使用了相同的代码,但脚本没有运行选项1和2中的脚本,而是要求这个

这是我使用的链接:如何在SQLPlus或PL/SQL中制作菜单?

PROMPT  1: Make a sales invoice 
PROMPT  2: Inquire a sales invoice    
accept selection PROMPT "Enter option 1-2: "
set term off
column script new_value v_script  --Q1. What's column script?
select case '&selection.'          --from accept above
when '1' then '@test1.sql'  --script to run when chosen option 1.
when '2' then '@test2.sql'  --script to run when chosen option 2.
else '@FinalAssignment.sql' --this script
end as script  --Q2. What script is this referring to? 
from dual; --Q3. Don't know this
set term on
@&v_script. --Q4. What script is being ran here?

这是我的输出

SQL> @myScript
1: Make a sales invoice
2: Inquire a sales invoice
Enter option 1-2: 1
Enter value for v_script: 1
SP2-0310: unable to open file "1.sql"

我的脚本文件的名称是"myScript",所以我认为如果输入了无效选项,菜单应该自行重新加载。然而,出于某种原因,它没有这样做。。

我不明白,而且我希望代码在循环中运行,即使当一个脚本被执行时,它也应该回到菜单并要求另一个选择。如果再次输入无效选项,则该选项应返回菜单,供用户选择其他选项。

我的代码包括循环在这里:

PROMPT  1: Make a sales invoice 
PROMPT  2: Inquire a sales invoice    
accept selection PROMPT "Enter option 1-2: "
set term off
LOOP
column script new_value v_script  --Q1. What's column script?
select case '&selection'          --from accept above
when '1' then @test1.sql --script to run when chosen option 1.
when '2' then @test2.sql  --script to run when chosen option 2.
when '3' then @test3.sql
EXIT WHEN &selection = 4;
else '@myScript.sql' --this script
end as script  --Q2. What script is this referring to? 
from dual; --Q3. Don't know this
END LOOP;
set term on
@&v_script. --Q4. What script is being ran here?

以下工作:

PROMPT  1: Make a sales invoice 
PROMPT  2: Inquire a sales invoice    
accept selection PROMPT "Enter option 1-2: "
column script new_value v_script
select case '&selection'
when '1' then '@test1.sql'
when '2' then '@test2.sql'
else '@FinalAssignment.sql'
end as script
from dual;
prompt '&v_script'
@&v_script

你在代码中的注释导致了问题(不知道你的脚本版本中是否有)

它所做的是提示一个值,并将其固定在一个名为"选择"的变量中。然后,它从dual中进行选择,并调用返回的列"script"-上一个"column"命令正在查找具有此名称的列,当它看到一个时,它会创建一个名为"v_script"的新变量,并将"script"列中的值放入其中

最后,这个变量被用作要调用的脚本的名称。

在sqlplus中创建循环是很棘手的,而且可能不是一件明智的事情。

通过您尝试的机制再次调用您的脚本也会进入sqlplus的调用深度(大约20个脚本,IIRC)——但不知道在以后的版本中是否已经删除/增加了这一深度。

请注意,"@"的意思是"在sqlpath中运行具有给定名称的脚本-在所选结果中再放一个"@"会将其变成"@@",意思是"运行当前目录中的脚本"。

最新更新