SQL*Plus how to accept text variable from prompt?

被刻印的时光 ゝ 提交于 2019-12-17 07:50:57

问题


Im very beginner in psql and i have a question.

Here is the code:

SET serveroutput ON
ACCEPT myVariable PROMPT "Input value: ";

BEGIN
  dbms_output.put_line('My input variable is: '||&myVariable);
 END;

Question is very simple: How can i pass text to my variable? If i input a number it is works correctly and i can read in the log my number, but if i pass a text like "mytext" instead of a number, i got an error:

    old:BEGIN


     dbms_output.put_line('My input variable is: '||&myVariable);
     END;


    new:BEGIN

  dbms_output.put_line('My input variable is: '||mytext);
 END;

    Error starting at line 5 in command:
    BEGIN
      dbms_output.put_line('My input variable is: '||&myVariable);
     END;
    Error report:
    ORA-06550: 2 sor, 50 oszlop:
    PLS-00201: identifier 'MYTEXT' must be declared
    ORA-06550: 2 sor, 3 oszlop:
    PL/SQL: Statement ignored
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:

回答1:


You have to specify the data type as part of the ACCEPT statement. If none is given, it assumes a number.

Try ACCEPT myVariable CHAR PROMPT 'Input value: '; instead.




回答2:


You have to enclose the character substitution variable in quotes when you use it, if it's a string, otherwise Oracle tries to interpret the value as an object name. You can see that on the 'new' version (shown because you have set verify on), and sexta13 alluded to that too. So you would do:

 dbms_output.put_line('My input variable is: '||'&myVariable');

But you don't need to concatenate the value in this case (whether it's a number or s string):

 dbms_output.put_line('My input variable is: &myVariable');



回答3:


You don't have MYTEXT variable declared anywhere.

dbms_output.put_line('My input variable is: '||mytext); -- here is the error. It should be &myVariable.


来源:https://stackoverflow.com/questions/16674252/sqlplus-how-to-accept-text-variable-from-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!