【问题标题】:Declaring, setting, and displaying a var in PL/SQL在 PL/SQL 中声明、设置和显示 var
【发布时间】:2012-10-12 18:52:48
【问题描述】:

我是 PL/SQL 的新手,但有很多其他 SQL 经验,包括 Oracle(只是没有那么多脚本)。我想声明一个数字(整数)变量,将其设置为行数,并将其显示在句子换行的字符串中。本练习的最终目标是创建一个 SQL*Plus 脚本来打印字符串“有 1 行”。

在 Unix 上的 SQL*Plus 中,我这样做:

SQL> variable v_dCnt number;
SQL> select count(*) into :v_dCnt from dual;

  COUNT(*)
----------
         1

SQL> select 'There are ' || :v_dCnt || ' rows' as MESSAGE from dual;

MESSAGE 
-------------------------------------------------------
There are  rows

注意它如何显示v_dCnt 的空白而不是值 1

在 Win7 上的 Rapid SQL 中,我会这样做

variable v_dCnt number;
select count(*) into :v_dCnt from dual;
select 'There are ' || :v_dCnt || ' rows' from dual;

并得到 ORA-01008: 并非所有变量都绑定

我做错了什么?

【问题讨论】:

  • 您确定 Rapid SQL 支持用于声明替换变量的 SQL*Plus 语法吗?
  • 不确定。但是 sqlplus 也不返回它应该返回的内容...
  • 所以 IOW,问题出在代码中,而不是客户端。最终,我的脚本将在 Unix 上用完 sqlplus。我包含 Rapid SQL 示例只是因为它实际上返回了一个有意义的错误消息,不像 sqlplus

标签: oracle plsql oracle11g sqlplus


【解决方案1】:

在 SQL*Plus 中,您很可能只需将 SELECT INTO 放入 PL/SQL 块中

SQL>  variable v_dCnt number;
SQL> begin
  2    select count(*)
  3      into :v_dCnt
  4      from dual;
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL> ed
Wrote file afiedt.buf

  1* select 'There are ' || :v_dCnt || ' rows' from dual
SQL> /

'THEREARE'||:V_DCNT||'ROWS'
-------------------------------------------------------
There are 1 rows

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多