PL/SQL 块 - 简单地查看输出? [我确信这是一个非常简单的问题!]
我确信我想要的很简单,但我不知道如何实现。
我想要:
- 声明一些变量并将它们初始化为某些值
- 要执行多个选择(由上述变量值预测)并查看结果,就像我直接在 sqlplus 命令行上执行结果一样
我相信有必要使用块结构,以便我可以在查询谓词中声明和使用变量。尽管此处显示的示例在实际情况中非常简单,但存在大量复杂得多的 SELECT。
我尝试这样做(暂时忘记了谓词)......
DECLARE
EMP_EMPLOYEE_ID_IN VARCHAR2(12);
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM DEPT WHERE DEPNO';
END;
/
但是当我这样做时,我可以执行选择而不会看到输出。
我也尝试过这个......
DECLARE
EMP_EMPLOYEE_ID_IN VARCHAR2(12);
BEGIN
SELECT * FROM DEPT;
END;
/
但后来我得到...
PLS-00428:此 SELECT 语句中需要一个 INTO 子句
...我真的不想为每一列声明一个变量这将出现在我的输出中。
谁能告诉我如何执行 SELECT,但可以简单轻松地看到输出,就像我在 sqlplus 命令行上一样,即看到与我执行此操作相同的输出
SQL> SELECT * FROM DEPT;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
谢谢,
我现在已经测试了下面 Shannon Severance 给出的答案并发现它会做我想做的事。
为了后来的读者,我认为在这里展示完整的脚本可能会有用。
set line 32000;
set trimspool on;
var V_CURSOR1 REFCURSOR;
var V_CURSOR2 REFCURSOR;
var V_CURSOR3 REFCURSOR;
DECLARE
DEPT_NUM_IN VARCHAR2(12);
BEGIN
DEPT_NUM_IN := '10';
OPEN :V_CURSOR1 FOR SELECT * FROM DEPT;
OPEN :V_CURSOR2 FOR SELECT * FROM DEPT ORDER BY LOC;
OPEN :V_CURSOR3 FOR SELECT * FROM DEPT WHERE DEPTNO = DEPT_NUM_IN ORDER BY LOC;
END;
/
print V_CURSOR1
print V_CURSOR2
print V_CURSOR3
I'm sure what I want is very simple but I cannot figure out how.
I want :
- To declare some variables and initialize them to certain values
- To excecute a number of selects (predicated by the above variable values) and see the results as if i had executed the results straight on the sqlplus command line
I believe it's necessary to use the block structure in order that I may declare and make use of variables within the predicates of the queries. Although the examples shown here are quite simple in the real case there are numberous, much more complex SELECT's.
I tried doing this (forgetting about predicates for a moment) ...
DECLARE
EMP_EMPLOYEE_ID_IN VARCHAR2(12);
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM DEPT WHERE DEPNO';
END;
/
... but when I do that I get to execute the select without seeing the output.
I've also tried this ...
DECLARE
EMP_EMPLOYEE_ID_IN VARCHAR2(12);
BEGIN
SELECT * FROM DEPT;
END;
/
... but then I get ...
PLS-00428: an INTO clause is expected in this SELECT statement
... I really don't want to have to declare a variable for every column which would appear in my output.
Can anyone tell me how I can execute the SELECTs but simply and easily see the output as if I were on the sqlplus command line, ie to see the same output as if I did this
SQL> SELECT * FROM DEPT;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Thanks
I have now tested the answer given by Shannon Severance below and found that it will do what I want.
For the sake of later readers I thought it might be useful to show the complete script here.
set line 32000;
set trimspool on;
var V_CURSOR1 REFCURSOR;
var V_CURSOR2 REFCURSOR;
var V_CURSOR3 REFCURSOR;
DECLARE
DEPT_NUM_IN VARCHAR2(12);
BEGIN
DEPT_NUM_IN := '10';
OPEN :V_CURSOR1 FOR SELECT * FROM DEPT;
OPEN :V_CURSOR2 FOR SELECT * FROM DEPT ORDER BY LOC;
OPEN :V_CURSOR3 FOR SELECT * FROM DEPT WHERE DEPTNO = DEPT_NUM_IN ORDER BY LOC;
END;
/
print V_CURSOR1
print V_CURSOR2
print V_CURSOR3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与sqlplus相比,其他工具可能有所不同。
首先声明一个 sqlplus refcursor 变量
然后在 PL/SQL 块中打开该游标,您可以在其中访问声明的变量和所有内容:
请注意上面的 refcursor 变量前面带有
:
,这是因为我们将 sqlplus 变量绑定到 PL/SQL 匿名块中接下来,使用 SQLPLUS 打印命令:
From sqlplus, other tools may be different.
First declare a sqlplus refcursor variable
Then open that cursor within a PL/SQL block, where you will have access to declared variables and everything:
Notice above that the refcursor variable is prepended with a
:
, this is because we are binding a sqlplus variable into the PL/SQL anonymous blockNext, use the SQLPLUS print command: