SQL * Plus how to accept a text variable from a prompt?

Im very new to 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; 

The question is very simple: how to pass text to a variable? If I enter a number, it works correctly, and I can read my number in the journal, but if I pass in a text, for example, "mytext" and not a number, I get 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: 
+6
source share
3 answers

You must specify the data type as part of the ACCEPT statement. If none is specified, it takes a number.

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

+9
source

You must enclose the character substitution variable in quotation marks when you use it if it is a string, otherwise Oracle tries to interpret the value as the name of the object. You can see this in the β€œnew” version (shown because you have set verify on ), and sexta13 also refers to this. So you would do:

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

But you do not need to concatenate the value in this case (be it a number or a string s):

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

You do not have the variable MYTEXT declared anywhere.

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

Source: https://habr.com/ru/post/945506/


All Articles