创建一个匿名块以显示 1 到 5 并出现以下错误

Creating a anonymous block to display 1 to 5 and getting the below error

ORA-06550: line 2, column 11:
PLS-00103: Encountered the symbol "=" when expecting one of the following:

   constant exception <an identifier>
   <a double-quoted delimited-identifier> table long double ref
   char time timestamp interval date binary national character
   nchar
The symbol "<an identifier>" was substituted for "=" to continue.
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

代码如下:

SET SERVEROUTPUT ON;
clear scr;
DECLARE v_counter := 0;
BEGIN
LOOP
v_counter:= v_counter+1;
IF v_counter=3 THEN CONTINUE; END IF;
EXIT WHEN v_counter=5;
END LOOP;
DBMS_OUTPUT.PUT_LINE('v_counter='||v_counter);
END;

Creating a anonymous block to display 1 to 5 and getting the below error

您的代码永远不会显示 1 到 5。正如@David 在评论中所说 -

  1. 您还没有为 variable 声明 data type
  2. DBMS_OUTPUTLOOP之外,因此它只会打印变量保存的最后一个值。
  3. 如果您只想打印 1 到 5,那么 IF-ELSE 构造似乎是不必要的。

你可以在一个 FOR LOOP -

中达到同样的效果
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
  2    FOR i IN 1..5
  3    LOOP
  4      DBMS_OUTPUT.PUT_LINE('v_counter='||i);
  5    END LOOP;
  6  END;
  7  /
v_counter=1
v_counter=2
v_counter=3
v_counter=4
v_counter=5

PL/SQL procedure successfully completed.

SQL>

同样可以在 SQL -

中完成
SQL> select 'v_counter ='||level counter from dual connect by level <=5;

COUNTER
---------------------------------------------------
v_counter =1
v_counter =2
v_counter =3
v_counter =4
v_counter =5

SQL>

问题是行读取

DECLARE v_counter := 0;

您忘记指定变量的数据类型。

应该是

DECLARE v_counter number := 0;

当然,如果您愿意,可以选择 pls_integer 而不是 number