我可以在DBMS_OUTPUT语句中调用单行函数查询吗?

时间:2016-11-16 05:03:34

标签: oracle plsql oracle-sqldeveloper

如何使用DBMS_OUTPUT.PUT_LINE

输出单行函数的结果
DBMS_OUTPUT.PUT_LINE(
  'Comma seperated list of columns: ' || (SELECT WM_CONCAT(COLUMN_NAME) FROM USER_CONS_COLUMNS)
);

这是在存储过程内部调用的。

2 个答案:

答案 0 :(得分:1)

不,你不能。

您需要添加一个额外的图层 - 一个变量,其中SQL查询的结果集将被绑定。 PL / SQL有一个特殊的结构。适用于此情况的构造是select into。有关详细信息,请参阅Query Result Set Processingthe fine manual

您的示例无法编译:

begin
  dbms_output.put_line((select dummy from dual));
end;
/

但是会产生PLS-00103:

  dbms_output.put_line((select dummy from dual));
                        *
ERROR at line 2:
ORA-06550: line 2, column 25:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternat
ORA-06550: line 2, column 49:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( ) , * % & = - + < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset mem

select into的一个工作示例:

declare
  v_dummy varchar2(32767);
begin
  -- the select statement can be arbitrary complex as long as it returns
  -- only one single row with a single column
  select dummy into v_dummy from dual;
  dbms_output.put_line(v_dummy);
end;
/

答案 1 :(得分:0)

您可以定义变量并存储选定的值,如以下代码块所示

DECLARE
   v_wm_concat user_cons_columns.column_name%TYPE;

BEGIN
   SELECT WM_CONCAT(COLUMN_NAME) FROM USER_CONS_COLUMNS INTO v_wm_concat
   DBMS_OUTPUT.PUT_LINE('Comma seperated list of columns: '|| v_wm_concat);
相关问题