This question already has answers here:
How to test an Oracle Stored Procedure with RefCursor return type?
(6 answers)
Closed 5 years ago.
I create the procedure to output multi rows and columns.
create or replace PROCEDURE MYPROC(
C1 OUT SYS_REFCURSOR )
AS
BEGIN
OPEN C1 FOR SELECT * FROM A_TABLE;
END MYPROC;
There is no error when I compiled it.
But I Can't call my procedure as normal like
Exec MYPROC;
I've got this error.
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'MYPROC'
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:
But when I run without script, it displayed my expected result.
So please help me how can I execute this procedure?
Thank You.
Error is because you are not passing any parameter to the procedure when it expects 1.
You need define a refcursor variable and then pass the variable into the procedure and finally read it.
var cur refcursor;
exec MYPROC(c1 => :cur);
print cur;
or
var cur refcursor;
begin
MYPROC(c1 => :cur);
end;
/
print cur;
Related
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;
/
I have created a procedure in oracle as follows
create or replace procedure jobsfetch
(id varchar2,jobcursor out sys_refcursor)
as
begin
open jobcursor for
select * from shop.jobs where job_id = id;
end;
I run the procedure in SQL*Plus using:
exec jobsfetch('AD_ASST');
But I'm getting the following error
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'JOBSFETCH'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
How to execute this procedure since its having only one input parameter?
try this:
variable CURSOR_LIST REFCURSOR;
exec jobsfetch('AD_ASST',:CURSOR_LIST);
print CURSOR_LIST;
You can simply use a plsql block:
declare
outCur sys_refcursor;
begin
jobsfetch('AD_ASST', outCur);
/* what you need to do with the cursor */
end;
The problem is that your procedure has an out parameter, but you don't supply it.
You can't exec jobsfetch('AD_ASST');
I recommend putting the whole thing in a plsql block like Aleksej.
declare
outCur sys_refcursor;
begin
jobsfetch('AD_ASST', outCur);
/* what you need to do with the cursor */
end;
This will give you the cursor that you opened in outCur and you can do whatever you need to do with it.
Using Oracle SQL Developer I created a simple procedure. The procedure compiles successfully, but when I type the command:
execute CMPPROJECTPROCSELECT();
BEGIN CMPPROJECTPROCSELECT(); END;
I get the following errors:
Error starting at line : 1 in command -
execute CMPPROJECTPROCSELECT()
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'CMPPROJECTPROCSELECT'
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:
Error starting at line : 2 in command -
BEGIN CMPPROJECTPROCSELECT(); END;
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'CMPPROJECTPROCSELECT'
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:
Please help me to solve this. I know it's a small error. Also I have specified the data types, declarations of names correctly.
My procedure code is
CREATE OR REPLACE PROCEDURE CMPPROJECTPROCSELECT(
p_projectname IN VARCHAR2,
p_description OUT VARCHAR2)
IS
BEGIN
SELECT DESCRIPTION
INTO p_description
FROM CMPPROJECT
WHERE PROJECTNAME = p_projectname;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_description:= NULL;
COMMIT;
END CMPPROJECTPROCSELECT;
execute CMPPROJECTPROCSELECT();
BEGIN CMPPROJECTPROCSELECT();
END;
EXECUTE is SQL*Plus command.
You are not passing the required parameters to the procedure. You have declared two parameters for your procedure:
p_projectname IN VARCHAR2,
p_description OUT VARCHAR2
So, you need to declare the required parameters and then pass it to the procedure:
DECLARE
proj_desc VARCHAR2(2000);
BEGIN
CMPPROJECTPROCSELECT('project_name', proj_desc);
-- use the OUT value of proj_desc later
END;
/
On a side note, you do not need COMMIT at all. It is required to permanently commit a DML and has nothing to do with a SELECT ..INTO clause.
SELECT DESCRIPTION INTO p_description FROM CMPPROJECT WHERE PROJECTNAME = p_projectname;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_description:= NULL;
COMMIT; -- You don't need COMMIT at all
UPDATE A working demonstration:
In PL/SQL:
SQL> CREATE OR REPLACE PROCEDURE get_emp(
2 p_ename IN VARCHAR2,
3 p_job OUT VARCHAR2)
4 IS
5 BEGIN
6 SELECT job INTO p_job FROM emp WHERE ename = p_ename;
7 END;
8 /
Procedure created.
SQL> sho err
No errors.
SQL> set serveroutput on
SQL> DECLARE
2 job VARCHAR2(20);
3 BEGIN
4 get_emp('SCOTT',JOB);
5 DBMS_OUTPUT.PUT_LINE('The output is '||job);
6 END;
7 /
The output is ANALYST
PL/SQL procedure successfully completed.
In SQL*Plus:
SQL> VARIABLE JOB VARCHAR2(20);
SQL> EXECUTE get_emp('SCOTT', :JOB);
PL/SQL procedure successfully completed.
SQL> PRINT JOB;
JOB
--------------------------------
ANALYST
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.
I created a procedure named greet as :
create procedure greet(message in char(50))
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
The procedure compiled successfully but when I try to call it as :
execute greet('Hey ! This is a self created procedure :)');
I get an error :
execute greet('Hey ! This is a self created procedure :)')
Error report:
ORA-06550: line 1, column 7:
PLS-00905: object SUHAIL.GREET is invalid
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:
What error is it ? Why do I get it ?
Note : 'suhail' is name of the current user connected to oracle server
I don't believe that your procedure compiled successfully. When I try to compile it on my system, I get syntax errors
SQL> create procedure greet(message in char(50))
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> sho err
Errors for PROCEDURE GREET:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/32 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= ) , default varying character large
The symbol ":=" was substituted for "(" to continue.
If I resolve the syntax errors (you cannot specify a length for an input parameter), it works
SQL> ed
Wrote file afiedt.buf
1 create or replace procedure greet(message in char)
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5* end;
SQL> /
Procedure created.
SQL> set serveroutput on;
SQL> execute greet('Hey ! This is a self created procedure :)');
Greet Message : Hey ! This is a self created procedure :)
PL/SQL procedure successfully completed.
I would be shocked if you really wanted the input parameter to be declared as CHAR. Almost always, you should use VARCHAR2 for character strings. It is exceptionally rare to come across a case where you really want the blank-padding semantics of a CHAR.
this is working dude;
create or replace
procedure greet(message in char)
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
see main property of char datatype is is the length of input data is less than the size you specified it'll add blank spaces.this case is not happened for varchar2.
in procedure above mentioned char property is violated so it's almost treat like varchar2. so if you remove size of input parameter it will work and also char support maximum length of input.