SQL*Plus how to accept text variable from prompt? - oracle

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:

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.

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');

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.

Related

Get the first value from Oracle cursor - Calling From Java Code

I have a oracle cursor which I have created to facilitate concurrency. This is my cursor.
create or replace FUNCTION get_unlocked_records RETURN table_to_test%ROWTYPE IS
CURSOR c IS SELECT * FROM table_to_test where status_code = 5 FOR UPDATE SKIP LOCKED;
record_to_get table_to_test%ROWTYPE;
BEGIN
OPEN c;
FETCH c INTO record_to_get;
CLOSE c;
RETURN record_to_get;
END;
When I do the testing in 2 separate sql sessions using these commands,it gives the following errors.
declare
record_to_gets table_to_test%ROWTYPE;
begin
exec :record_to_gets := get_unlocked_records;
dbms_output.put_line(record_to_gets);
end;
Error
Error starting at line : 32 in command -
declare
record_to_gets table_to_test%ROWTYPE;
begin
exec :record_to_gets := get_unlocked_records;
dbms_output.put_line(record_to_gets);
end;
Error report -
ORA-06550: line 4, column 7:
PLS-00103: Encountered the symbol "" when expecting one of the following:
:= . ( # % ;
The symbol ";" was substituted for "" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What is the error that I am doing here ?
Since my ultimate goal is to call the function and get the result in java, how to call this function to get the first record in java ?
Thanks in advance.
EXEC[UTE] is a SQL*Plus command and prepending variable with a colon is done in SQL*Plus, but in PL/SQL EXECUTE IMMEDIATE might be used whereas that's not needed in your case, only using such an assignment without prepending the local variable is enough :
DECLARE
record_to_gets table_to_test%ROWTYPE;
BEGIN
record_to_gets := get_unlocked_records;
DBMS_OUTPUT.PUT_LINE(record_to_gets.col1);
DBMS_OUTPUT.PUT_LINE(record_to_gets.col2)
END;
/

execute immediate ora-01704 string literal too long

I have the following code
BEGIN
EXECUTE IMMEDIATE 'CALL name_package.name_procedure('string literal too long...')';
END;
when I run it I get the following error:
ORA-01704: string literal too long
ORA-06512: line 2
01704. 00000 - "string literal too long"
*Cause: The string literal is longer than 4000 characters.
*Action: Use a string literal of at most 4000 characters.
Longer values may only be entered using bind variables.
I have tried with a variable clob but the error persists, some idea of how to solve this issue, i am using oracle 11g
Use a bind variable:
DECLARE
str CLOB := EMPTY_CLOB();
BEGIN
-- Make a long (random) string:
FOR i IN 1 .. 10 LOOP
str := str || DBMS_RANDOM.STRING( 'a', 4000 );
END;
-- Pass it into the dynamic SQL:
EXECUTE IMMEDIATE 'CALL name_package.name_procedure( :1 )' USING str;
-- Or just call the procedure without dynamic SQL:
name_package.name_procedure( str );
END;
Unclear what you are trying to accomplish. Within a PL/SQL block you would need only reference name_package.name_procedure('string'). Assuming name_procedure is defined in the specification of name_package.
BEGIN
name_package.name_procedure('string');
END;
CLOB variable would work in place of 'string' but only if that parameter is defined as CLOB and not as VARCHAR2.
EXECUTE IMMEDIATE requires a string. Quotes within a quoted string need to be presented twice.

Error user input PLS-00201: identifier 'ISS' must be declared [duplicate]

This question already has an answer here:
PLS-00201: identifier 'USER INPUT' must be declared
(1 answer)
Closed 10 months ago.
Why do I get this error? If the variable declaration is the same type as the column shouldn't this work. The nursing_unit_id is a varchar2(10). It worked like this for me when the ID was a NUMBER;
Error I get:
Error report -
ORA-06550: line 13, column 13:
PLS-00201: identifier 'ISS' must be declared
SET SERVEROUTPUT ON;
DECLARE
v_unit ADMISSIONS.NURSING_UNIT_ID%TYPE := &v_unit;
v_admissions ADMISSIONS%ROWTYPE;
CURSOR c_admissions IS
SELECT *
INTO v_admissions
FROM ADMISSIONS
WHERE NURSING_UNIT_ID = v_unit;
BEGIN
FOR r_admissions IN c_admissions
LOOP
DBMS_OUTPUT.PUT_LINE(r_admissions );
END LOOP;
END;
If nursing_unit_id is varchar2(10) and you've defined the substitution variable &v_unit as ISS, then you have to enclose the reference to the substitution variable in quotes, to make it a string:
v_unit ADMISSIONS.NURSING_UNIT_ID%TYPE := '&v_unit';
If you run your code in SQL*Plus or SQL Developer with set verify on, you'll see the before and after values for that substitution, which would show it it trying to to do:
v_unit ADMISSIONS.NURSING_UNIT_ID%TYPE := ISS;
instead of what you'd get with it quoted:
v_unit ADMISSIONS.NURSING_UNIT_ID%TYPE := 'ISS';
You will still get other errors; the next one is:
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
You've defined v_admissions as a row type, which means it's a record. You can't write a whole record out in one go with dbms_output, you have to refer to each field separately, e.g. (using your amended code, where `r_admissions is also implicitly of that same row type):
DBMS_OUTPUT.PUT_LINE(r_admissions.nursing_unit_id);
Having an into clause in the cursor definition isn't useful; it isn't actually populated, so you can remove that clause and the v_admissions variable declaration. You don't really need the v_unit local variable either, you can use the substitution variable directly; and there are various cursor forms you can use, such as:
SET SERVEROUTPUT ON;
BEGIN
FOR r_admissions IN (
SELECT *
FROM ADMISSIONS
WHERE NURSING_UNIT_ID = '&v_unit'
)
LOOP
DBMS_OUTPUT.PUT_LINE(r_admissions.nursing_unit_id);
-- and anything else you want to do with that row's data
END LOOP;
END;
/

Calculate the substitution variable length in oracle plsql

I have simple plsql program, basically what I would like to do is to calculate the length of the name which is dynamic input to plsql and then loop based on the length.
When I give pre-defined value like v_name = 'Dex' it works but when I give v_name = &name it throws an error message saying Dex must be declared.
Appreciate if any one can shed light on this issue. Please find error and program details below:
Error report:
ORA-06550: line 6, column 13:
PLS-00201: identifier 'DEX' must be declared
ORA-06550: line 6, column 3:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
PLsql Program:
declare
v_name varchar2(30);
v_loop_count number;
v_len number;
begin
v_name := &name;
v_loop_count := 0;
v_len := length(v_name);
for v_loop_count in 0 .. v_len-1
loop
dbms_output.put_line('Entered Name is :: '||v_name);
end loop;
end;
In SQL Developer (and in sqlplus), you can define substitution variables such as you're doing that start with &. But these substitution variables are unlike variables or literals in your code. These substitution variables will only be replaced textually, before the code gets parsed and executed.
If you have
v_name := &name;
Then the textual replacement will yield this code:
v_name := Dex;
Because you don't have a variable named DEX, you get the error you mentioned.
As mentioned in the comments, placing single-quotes around the substitution variable is the answer.
v_name := '&name';
This will get replaced with
v_name := 'Dex';
which is what you wanted -- the string value 'Dex'.

Stored procedure expects parameter when no IN parameters are defined

I have this very simple stored procedure in Oracle that executes a sequence and gives the next sequence number as output.
create or replace PROCEDURE NEXT_NUMBER
(SEQUENCE_OUT OUT NUMBER)
IS
BEGIN
EXECUTE IMMEDIATE 'SELECT TEST_SEQUENCE.NEXTVAL FROM DUAL' INTO sequence_out;
END;
As you can see, there are no IN parameters to this procedure so I'm puzzled when I execute this procedure like this: execute CRS_NEXT_CRC_NUMBER;
and I get the following error:
Error starting at line : 1 in command -
execute NEXT_NUMBER
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'CRS_NEXT_CRC_NUMBER'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Any idea why this could be happening? I can call the NEXTVAL function on the SEQUENCE outside of the procedure without a problem.
create or replace PROCEDURE NEXT_NUMBER
(SEQUENCE_OUT OUT NUMBER)
IS
BEGIN
SELECT TEST_SEQUENCE.NEXTVAL INTO sequence_out FROM DUAL;
-- or simply (in newer Oracle releases)
sequence_out := TEST_SEQUENCE.NEXTVAL;
END;
In SQLPLUS:
> var ID NUMBER
> exec NEXT_NUMBER(:ID);
> print ID
PLS-00306: wrong number or types of arguments in call to 'CRS_NEXT_CRC_NUMBER'
You have not declared the OUT parameter and the program expects an argument, i.e. the OUT parameter SEQUENCE_OUT.
You could execute the procedure with the OUT parameter in two ways.
SQL*Plus variable
Anonymous block
In SQL*Plus:
var SEQUENCE_OUT number
exec next_number(:SEQUENCE_OUT);
print SEQUENCE_OUT
Anonymous block:
SET SERVEROUTPUT ON
DECLARE
SEQUENCE_OUT NUMBER;
BEGIN
next_number(SEQUENCE_OUT);
DBMS_OUTPUT.PUT_LINE(SEQUENCE_OUT);
END;
/
You done almost correct. You need to call method like
execute NEXT_NUMBER(:val);
You don't set out parameter, which will take out value.

Resources