PL/SQL 块 - 简单地查看输出? [我确信这是一个非常简单的问题!]

发布于 2024-12-02 01:27:44 字数 1549 浏览 1 评论 0原文

我确信我想要的很简单,但我不知道如何实现。

我想要:

  • 声明一些变量并将它们初始化为某些值
  • 要执行多个选择(由上述变量值预测)并查看结果,就像我直接在 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 技术交流群。

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

发布评论

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

评论(2

姜生凉生 2024-12-09 01:27:44

与sqlplus相比,其他工具可能有所不同。

首先声明一个 sqlplus refcursor 变量

SQL> var l_cursor refcursor

然后在 PL/SQL 块中打开该游标,您可以在其中访问声明的变量和所有内容:

SQL> edit
Wrote file afiedt.buf

  1  declare
  2      l_number number;
  3  begin
  4      open :l_cursor for select table_name from all_tables where rownum < 10;
  5* end;
SQL> /

PL/SQL procedure successfully completed.

请注意上面的 refcursor 变量前面带有 :,这是因为我们将 sqlplus 变量绑定到 PL/SQL 匿名块中

接下来,使用 SQLPLUS 打印命令:

SQL> print l_cursor

TABLE_NAME
------------------------------
ICOL$
CON$
UNDO$
PROXY_ROLE_DATA$
FILE$
UET$
IND$
SEG$
COL$

9 rows selected.

From sqlplus, other tools may be different.

First declare a sqlplus refcursor variable

SQL> var l_cursor refcursor

Then open that cursor within a PL/SQL block, where you will have access to declared variables and everything:

SQL> edit
Wrote file afiedt.buf

  1  declare
  2      l_number number;
  3  begin
  4      open :l_cursor for select table_name from all_tables where rownum < 10;
  5* end;
SQL> /

PL/SQL procedure successfully completed.

Notice above that the refcursor variable is prepended with a :, this is because we are binding a sqlplus variable into the PL/SQL anonymous block

Next, use the SQLPLUS print command:

SQL> print l_cursor

TABLE_NAME
------------------------------
ICOL$
CON$
UNDO$
PROXY_ROLE_DATA$
FILE$
UET$
IND$
SEG$
COL$

9 rows selected.
笑忘罢 2024-12-09 01:27:44
DECLARE
  v_result VARCHAR2(400);

BEGIN

  SELECT dummy
    INTO v_result
    FROM dual;

  dbms_output.put_line(v_result);

END;
DECLARE
  v_result VARCHAR2(400);

BEGIN

  SELECT dummy
    INTO v_result
    FROM dual;

  dbms_output.put_line(v_result);

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