在PL/SQL中使用替代变量时,如果直接回车的话,发现赋的值即不是''也不是NULL
DECLARE
v_test NUMBER;
BEGIN
v_test := &v_test;
DBMS_OUTPUT.PUT_LINE('result' || v_test);
END;
/如果直接回车的话,就会出错,&v_test给的不是NULL也不是'',而是无,就相当变成v_test := ;这样
这要怎么解决这个问题?

解决方案 »

  1.   


    DECLARE
    v_test NUMBER;
    BEGIN
    v_test := &v_test;--这只是一个赋值语句,
                      --如果你直接回车,那么这个语句等于:v_test := ;这是错误的语句,
                      --也没什么意义
    if v_test is null then
       DBMS_OUTPUT.PUT_LINE('result is null');
    else 
       dbms_output.put_line('result is '||v_test);
    end if;
    END;
    /
    1.result is null
    PL/SQL procedure successfully completed2.result is 10010
    PL/SQL procedure successfully completed
      

  2.   


    SQL> set serveroutput on;
    SQL> DECLARE
      2  v_test NUMBER;
      3  BEGIN
      4  v_test := &v_test;
      5  if v_test is null then
      6     DBMS_OUTPUT.PUT_LINE('result is null');
      7  else 
      8     dbms_output.put_line('result is '||v_test);
      9  end if;
     10  END;
     11  /
    Enter value for v_test: null
    old   4: v_test := &v_test;
    new   4: v_test := null;
    result is nullPL/SQL procedure successfully completed.SQL> /
    Enter value for v_test: 
    old   4: v_test := &v_test;
    new   4: v_test := ;--解析到这里就出了问题
    v_test := ;
              *
    ERROR at line 4:
    ORA-06550: line 4, column 11:
    PLS-00103: Encountered the symbol ";" when expecting one of the following:
    ( - + case mod new not null <an identifier>
    <a double-quoted delimited-identifier> <a bind variable> avg
    count current exists max min prior sql stddev sum variance
    execute forall merge time timestamp interval date
    <a string literal with character set specification>
    <a number> <a single-quoted SQL string> pipe
    <an alternatively-quoted string literal with character set specification>
    <an alternatively-quoted S
    SQL> 
      

  3.   

    回车键的ascii码为:13
    select ascii(to_char(13)) from daul;
      

  4.   

    SQL> DECLARE
      2    v_test NUMBER;
      3  BEGIN
      4    v_test := '&v_test';
      5    DBMS_OUTPUT.PUT_LINE('result=' || v_test);
      6  END;
      7  /
    输入 v_test 的值:  
    原值    4:   v_test := '&v_test';
    新值    4:   v_test := '';
    result=PL/SQL 过程已成功完成。SQL> /
    输入 v_test 的值:  10
    原值    4:   v_test := '&v_test';
    新值    4:   v_test := '10';
    result=10PL/SQL 过程已成功完成。SQL>