无法使用PL / SQL表达式

时间:2018-07-24 19:19:31

标签: sql oracle plsql

尝试运行以下sql时,出现以下错误:

错误:PLS-00363:表达式“ PNUM”不能用作分配目标 线:622

Procedure insertRec(pNum In VARCHAR2
                    ,pName in VARCHAR2)
Is
Begin

    IF  pNum ='0' Then
      pNum = '100';
   End;

  Insert into insertNum(Num,Name) values(pNum,pName);    

End;

2 个答案:

答案 0 :(得分:4)

pnumIN参数;为什么将其设置为其他值?如果您坚持要这样做,请将其设置为IN OUT参数,这会使事情变得复杂,因为您不仅可以调用该过程,还需要声明一个将传递给该过程的变量(如您必须将OUT值放在某处)。

此外,您没有为=分配值,而是为:=(即pnum := '100';)分配了值。

这是一个例子:

SQL> create table insertnum (num varchar2(10), name varchar2(20));

Table created.

SQL> create or replace procedure insertrec (pnum in out varchar2, pname in varchar2) is
  2  begin
  3    if pnum = '0' then
  4       pnum := '100';
  5    end if;
  6    insert into insertnum (num, name) values (pnum, pname);
  7  end ;
  8  /

Procedure created.

SQL>
SQL> declare
  2    l_n varchar2(10) := '0';
  3  begin
  4    insertrec(l_n, 'x');
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from insertnum;

NUM        NAME
---------- --------------------
100        x

SQL>

但是,应使用DECODE(或CASE)将其重写为

SQL> create or replace procedure insertrec (pnum in varchar2, pname in varchar2) is
  2  begin
  3    insert into insertnum (num, name) values (decode(pnum, '0', '100', pnum), pname);
  4  end ;
  5  /

Procedure created.

SQL> begin
  2    insertrec('20', 'y');
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> select * From insertnum;

NUM        NAME
---------- --------------------
100        x
20         y

SQL>

答案 1 :(得分:1)

您不能更改IN参数的值。而是声明一个变量。

PROCEDURE insertRec(pNum IN VARCHAR2, pName IN VARCHAR2)
IS
    num pNum%Type;
BEGIN
    num := pNum;
    IF pNum ='0' THEN
        num := '100';
    END;

    INSERT INTO insertNum(Num,Name) VALUES(num,pName);    
END;