如何在 SQLPlus 或 PL/SQL 中创建菜单
我有几个脚本,我想从提供给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面是执行此操作的 SQL Plus 脚本:
注意 case 表达式的 ELSE 部分中的“菜单”是该脚本的名称,以便当用户输入无效选项时它会再次运行。
Here is a SQL Plus script to do that:
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.
使用 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.
您可以从主脚本执行脚本:
&variablename
用于引用 SQLPlus 中的变量variablename
,与$variablename
的方式大致相同用于 shell 脚本中。如果variablename
未定义,则SQLPlus会提示用户输入一个值。您可以提供相对于主脚本的路径 - 该示例依赖于位于同一目录中的支持脚本。
You can execute scripts from a master script:
&variablename
is used to refer to the variablevariablename
in SQLPlus, much the same way$variablename
is used in shell scripts. Ifvariablename
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.
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.
我对此有一种真正的语言(根据巴勃罗的评论)。
您可以在 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.
如果脚本完全不相关,我会使用简单的批处理文件或 shell 脚本。
If the scripts are totally unrelated, I'd use a simple batch file or shell script.