如何在 SQLPlus 或 PL/SQL 中创建菜单

发布于 2024-08-13 17:43:29 字数 128 浏览 10 评论 0原文

我有几个脚本,我想从提供给 SQLPlus 用户的菜单中启动它们。类似于:

请选择:
1:编写脚本
2:执行脚本b
3:执行脚本c

我只需要一个正确方向的点,而不是一个快速答案。

I have several scripts that I would like to start from a menu presented to the SQLPlus user. Something like:

Please make a selection:
1: Do script a
2: Do script b
3: Do script c

I just need a point in the right direction, not a quick answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甩你一脸翔 2024-08-20 17:43:29

下面是执行此操作的 SQL Plus 脚本:

prompt Please make a selection:
prompt 1: Do script a
prompt 2: Do script b
prompt 3: Do script c

accept selection prompt "Enter option 1-3: "

set term off

column script new_value v_script

select case '&selection.'
       when '1' then 'script_a'
       when '2' then 'script_b'
       when '3' then 'script_c'
       else 'menu'
       end as script
from dual;

set term on

@&v_script.

注意 case 表达式的 ELSE 部分中的“菜单”是该脚本的名称,以便当用户输入无效选项时它会再次运行。

Here is a SQL Plus script to do that:

prompt Please make a selection:
prompt 1: Do script a
prompt 2: Do script b
prompt 3: Do script c

accept selection prompt "Enter option 1-3: "

set term off

column script new_value v_script

select case '&selection.'
       when '1' then 'script_a'
       when '2' then 'script_b'
       when '3' then 'script_c'
       else 'menu'
       end as script
from dual;

set term on

@&v_script.

NB The 'menu' in the ELSE part of the case expression is the name of this script, so that it runs itself again when the user enters an invalid option.

鱼忆七猫命九 2024-08-20 17:43:29

使用 SQLPlus 和/或 PL/SQL 很难完成您想要做的事情。

SQLPlus 是 Oracle 数据库的前端。其主要目的是针对 Oracle RDBMS 执行查询。

PL/SQL 是一种在 Oracle 中操作数据的语言。

相反,如果您想要与用户交互的东西,我建议您使用 Oracle 连接编写一个小脚本/程序在此处插入您最喜欢的语言(可以是 python、C/C++、C#、Java)执行您需要的 SQL 查询或 PL/SQL 程序。

It's hard to accomplish what you are trying to do with SQLPlus and/or PL/SQL.

SQLPlus is a frontend for Oracle databases. Its main purpose is to perform queries against an Oracle RDBMS.

PL/SQL is a language to manipulate data in Oracle.

Instead, if you want something with user interaction, I would suggest you to write a little script/program in insert your favorite language here (could python, C/C++, C#, Java) with an Oracle connection to perform the SQL queries or PL/SQL programs you need.

淡水深流 2024-08-20 17:43:29

您可以从主脚本执行脚本:

CASE LOWER(&v_script_selection)
  WHEN 'a' THEN
    @script_a.sql
  WHEN 'b' THEN
    @script_b.sql
  WHEN 'c' THEN
    @script_c.sql
  ELSE
    DBMS_OUTPUT('No such option available')
END

&variablename 用于引用 SQLPlus 中的变量 variablename,与 $variablename 的方式大致相同用于 shell 脚本中。如果variablename未定义,则SQLPlus会提示用户输入一个值。

您可以提供相对于主脚本的路径 - 该示例依赖于位于同一目录中的支持脚本。

You can execute scripts from a master script:

CASE LOWER(&v_script_selection)
  WHEN 'a' THEN
    @script_a.sql
  WHEN 'b' THEN
    @script_b.sql
  WHEN 'c' THEN
    @script_c.sql
  ELSE
    DBMS_OUTPUT('No such option available')
END

&variablename is used to refer to the variable variablename in SQLPlus, much the same way $variablename is used in shell scripts. If variablename is undefined, then SQLPlus prompts the user for a value.

You can provide a path relative to the master script - the example relies on the supporting scripts to be in the same directory.

清醇 2024-08-20 17:43:29

DBMS_OUTPUT可用于打印线到屏幕上。看起来它也有执行 GET_LINE 的函数,但我从未使用过它,也不知道它们的工作效果如何。

您只需要小心 SQLPlus 设置即可。如果您设置不正确,它可能会截断行。

DBMS_OUTPUT can be used to print lines to the screen. It looks like it has has functions that do a GET_LINE as well, but I've never used it and don't know how well they work.

You just need to be careful about your SQLPlus settings. It can truncate lines if you don't set it up properly.

锦欢 2024-08-20 17:43:29

我对此有一种真正的语言(根据巴勃罗的评论)。

您可以在 login.sql 中添加一些内容,在连接时显示列表建议(PRINT 或 PROMPT SQL*Plus 语句)。

您可以有一堆名为 1.sql、2.sql 等的脚本,如果用户输入 @1、@2 等(只要它们位于正确的目录中),这些脚本就会运行。

但实际上 SQL*Plus 并不适合于此。

I'd got with a real language for this (as per Pablo's comment).

You could have some stuff in a login.sql that displayed a list suggestions when you connect (PRINT or PROMPT SQL*Plus statements).

And you could have a bunch of scripts called 1.sql, 2.sql etc which would get run if the user entered @1, @2 etc. (as long as they are in the correct directory).

But really SQL*Plus isn't suited for this.

迟到的我 2024-08-20 17:43:29

如果脚本完全不相关,我会使用简单的批处理文件或 shell 脚本。

If the scripts are totally unrelated, I'd use a simple batch file or shell script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文