如何使匿名阻止工作

时间:2016-11-24 12:12:19

标签: oracle plsql block

假设我有这个ksh:

$ORACLE_HOME/bin/sqlplus -s u/p <<EOF
  WHENEVER OSERROR EXIT 98;
  WHENEVER SQLERROR EXIT SQL.SQLCODE;

  set heading on
  set feedback on
  set echo on
  set pagesize 0

  SELECT field1 FROM MY.TABLE;
EOF

选择是:没关系

现在让我们在查询周围添加一个匿名块:

DECLARE
    VARIABLE VARCHAR2(2) := 99
BEGIN
    prompt before select
    SELECT field1 INTO VARIABLE FROM MY.TABLE;
    prompt after select
    EXIT NUMCARSAT;
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        prompt NO_DATA_FOUND
        EXIT 99;
    WHEN TOO_MANY_ROWS THEN
        prompt TOO_MANY_ROWS
        EXIT 99;
END;

不再执行查询。那是为什么?

3 个答案:

答案 0 :(得分:0)

在SQL中,您只需运行查询:

SQL> select 1 from dual;

         1
----------
         1

但是在PLSQL块中,您需要使用查询来构建游标,执行循环,将数据提取到某个变量中,...

例如:

SQL> begin
  2    select 1 from dual;
  3  end;
  4  /
  select 1 from dual;
  *
ERROR at line 2:
ORA-06550: line 2, column 3:
PLS-00428: an INTO clause is expected in this SELECT statement


SQL> declare
  2    a number;
  3  begin
  4    select 1 into a from dual;
  5  end;
  6  /

PL/SQL procedure successfully completed.

答案 1 :(得分:0)

您使用的语法未在oracle plsql中使用。正确的语法如下:

DECLARE
    VAR VARCHAR2(2) := 99 ;
BEGIN
    dbms_output.put_line('before select');

    SELECT field1 
     INTO VAR 
     FROM MY.TABLE;

    --dbms_output.put_line is used to print to console
    dbms_output.put_line('before select');
    dbms_output.put_line('value ='||var);

EXCEPTION
    WHEN NO_DATA_FOUND THEN
        dbms_output.put_line('NO_DATA_FOUND');
    WHEN TOO_MANY_ROWS THEN
         dbms_output.put_line('TOO_MANY_ROWS ');
END;

答案 2 :(得分:0)

首先,您不能在PL / SQL块中使用“prompt”语句,因此语法无效。

其次,你需要在DECLARE / BEGIN / END PL / SQL块之后的单独行上附加一个斜杠,否则SQL * Plus将等待更多输入,如下所示:

BEGIN
     DBMS_OUTPUT.PUT_LINE('none of this will be parsed until slash below appears on standard input');
END;
/