Oracle DB:如何将函数结果存储到变量内部程序中

Evg*_*175 2 oracle plsql

美好的一天.我有一个功能:

create function get_n(search tt.pp%type)
  return number
  is rc number;
begin
  select count(*)
  into rc
  from tt
  where tt.pp=search;

  return (rc);
end;
/
Run Code Online (Sandbox Code Playgroud)

我可以得到结果

variable qwe number;

begin
  select get_n('sample')
    into :qwe
    from dual;
end;

print qwe;
Run Code Online (Sandbox Code Playgroud)

所以,它成功运作.但是部分:我print执行其他时不能执行(PLS-00103:遇到符号"PRINT"......).这真的很奇怪.

我尝试从匿名块中的函数获取结果并打印它:

declare
  qwe number;
begin
  select get_n('sample')
    into :qwe
    from dual;
  dbms_output.put_line(qwe);
exception
  when others
    then dbms_output.put_line(sqlerrm);
end;
/
Run Code Online (Sandbox Code Playgroud)

它不打印任何东西.为什么?

Kac*_*per 5

问题是:.以下代码应该工作:

declare
  qwe number;
begin
  select get_n('sample')
    into qwe
    from dual;
  dbms_output.put_line(qwe);
exception
  when others
    then dbms_output.put_line(sqlerrm);
end;
/
Run Code Online (Sandbox Code Playgroud)

:表示需要绑定的变量,而不是PL/SQL块内的变量.
如果第一个块/在PL/SQL块之后丢失了什么原因导致编译器读取print作为PL/SQL的一部分而不是SQLplus脚本:

variable qwe number;

begin
  select get_n('sample')
    into :qwe
    from dual;
end;
/

print qwe;
Run Code Online (Sandbox Code Playgroud)