PLSQL: Declare does not work



我使用

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE    11.2.0.3.0  Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production

我想声明一个变量以便稍后使用它。我是这样做的:

DECLARE
  price  myBeer VARCHAR(20);;

这会导致错误:

[DECLARE - 0 row(s), 0.000 secs]  [Error Code: 6550, SQL State: 65000]  ORA-06550: Row 2, Column 22:
PLS-00103: found symbol "end-of-file" while expecting one of the following: := ; not null default character

这让我做了以下事情:

DECLARE
myBeer VARCHAR(20) :=2;

但这也会导致错误:

[DECLARE - 0 row(s), 0.000 secs]  [Error Code: 6550, SQL State: 65000]  ORA-06550: Row 2, Column 26:
PLS-00103: found symbol "end-of-file" while expecting one of the following: * & = - + ; < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset

这真的有那么难吗?

您必须一次编译整个块,并且变量仅在BEGIN ... END块的范围内有效:

DECLARE
  myVariableName  VARCHAR2(4000);
BEGIN
  myVariableName := 'TEST';
  DBMS_OUTPUT.PUT_LINE( 'Value of var = ' || myVariableName ); -- This works!
END;
/
-- This will not work, since it is outside of the Block!
SELECT myVariableName FROM DUAL; 

下面的代码可以正常工作:

DECLARE
  price  VARCHAR(20);
begin   
    null;  
end;  

似乎你的代码语法不正确。

编辑-我张贴这段代码只是为了使它编译。

最新更新