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.
Related
create or replace procedure test(supplierid in number,
supplier out varchar) is
begin
select first_name
into Supplier
from lup_sup_master
where sup_id = supplierid;
end;
/
execute test(2279, :supplierid);
or
begin
execute test(2279, :supplierid); dbms_output.enable; dbms_output.put_line(supplier);
end;
Your procedure test has 2 input arguments. One has type IN which is supposed to be used with the procedure and the other parameter supplier has type OUT which means this parameter is suppose to hold the value which the Procedure returns.
As explained by #Barbaros, if you want to execute the Procedure via a SQL command prompt, you can follow the steps.
However the second way you showed was using a PLSQL Block.
begin
execute test(2279, :supplierid);
dbms_output.enable;
dbms_output.put_line(supplier); end;
In the above example of calling, you must note that Execute keyword is only be used whenever you use a SQL command line prompt. While using a PLSQL block you cal directly call the Procedure by its name as shown below. Also note that incase you have a OUT parameter, you must have OUT parameter passed to the Procedure since the Procedure is expecting 2 arguments. See below demo.
declare
var varchar2(100);
begin
test( supplierid => 2279, supplier =>var);
dbms_output.enable;
dbms_output.put_line(var);
End;
It seems you're confused between supplierid and supplier. You need to define a variable for supplier to get an output line printed, and most probably you defined a command line variable supplierid which's numeric type.
So, use below :
SQL> set serveroutput on;
SQL> var supplier varchar2;
SQL> execute test(2279, :supplier);
supplier
---------
Afzal -- as an example name
This is my code:
create or replace procedure p1
as
begin
create table emp_1 as (select * from emp);
end;
sql>exec p;
Then I get this error:
as ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'P1'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
You have several unclear issues :
Your procedure is p1 and you execute p. Why?
You can't execute create table statement inside a procedure like select or other DML. Use "EXECUTE IMMEDIATE" statement for that.
Why you are trying to create the table inside the procedure ? You can execute the statement directly with no procedure.
Try this ....
create or replace procedure p
as
begin
execute immediate 'create table emp_1 as (select * from emp)';
end;
sql>exec p;
I am trying to execute a procedure which in turn should execute four other procedures one after the other. How do I acheive this?
Create or replace procedure mainproc
as
begin
tack(400);
phno_insert;
address_insert;
academics_insert;
commit;
end;
Error report:
PLS-00905: Object phno_insert is invalid. PL/SQL: Statement ignored.
PLS-00905: Object address_insert is invalid. PL/SQL: Statement
ignored. PLS-00905: Object academics_insert is invalid. PL/SQL:
Statement ignored.
The problem seems to be in the fact that you have a procedure doing a DDL over an object that is statically referenced in another procedure; for example, if I define:
create table runtimeTable as select 1 as one from dual;
create or replace procedure createTable is
begin
execute immediate 'drop table runtimeTable';
execute immediate 'create table runtimeTable as select 1 as one from dual';
end;
create or replace procedure useTable is
vVar number;
begin
select one
into vVar
from runtimeTable;
--
dbms_output.put_line(vVar);
end;
create or replace procedure createAndUseTable is
begin
createTable;
useTable;
end;
/
when I try to execute createAndUseTable I get:
ORA-04068: existing state of packages has been discarded ORA-04065:
not executed, altered or dropped stored procedure "ALEK.USETABLE"
ORA-06508: PL/SQL: could not find program unit being called:
"ALEK.USETABLE" ORA-06512: at "ALEK.CREATEANDUSETABLE", line 4
ORA-06512: at line 1
If you strictly need to do a DDL runtime, you need to use dynamic SQL to reference the modified object; for example if I define the procedure useTable this way
create or replace procedure useTable is
vVar number;
begin
execute immediate
'select one
from runtimeTable'
into vVar;
--
dbms_output.put_line(vVar);
end;
the call to createAndUseTable will work:
SQL> exec createAndUseTable
1
I defined a stored procedure
create or replace procedure spfirst
(
sp_loc out varchar,
sp_sal out int
)
as
begin
select LOCATION, MONTHLY_SALARY
into sp_loc, sp_sal
from nilesh;
end;
I then call the procedure and get an error
begin
spfirst;
end;
ORA-06550: line 2, column 1:
PLS-00201: identifier 'SPNAME' must be declared
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
Your procedure takes two parameters. You are calling it without any parameters. Oracle thus looks for a procedure named spfirst that takes no parameters, finds no such procedure, and throws an error.
Something like
DECLARE
l_location nilesh.location%type;
l_salary nilesh.monthly_salary%type;
BEGIN
spfirst( l_location, l_salary );
END;
should work. Of course, you'd generally want to do something with the variables that are returned. If you've enabled dbms_output, you could print them out
DECLARE
l_location nilesh.location%type;
l_salary nilesh.monthly_salary%type;
BEGIN
spfirst( l_location, l_salary );
dbms_output.put_line( 'Location = ' || l_location );
dbms_output.put_line( 'Salary = ' || l_salary );
END;
Be aware that your procedure will throw an error unless the nilesh table has exactly one row. It seems likely that you either want the procedure to take an additional parameter that is the key to the table so that the select into always returns a single row or that you want a function that returns a sys_refcursor rather than a procedure that has multiple out parameters.
I created a procedure in Oracle but am getting an error when I attempt to execute it. Below are listed the steps I am taking to create this procedure:
SQL> ed getuserinfo
create or replace procedure getUserInfo
( p_username out Users.username%TYPE,
p_password out Users.password%TYPE ) IS
BEGIN
select username,password into p_username,p_password from users where username='yogi';
END;
/
SQL> exec getuserinfo
BEGIN getuserinfo; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'GETUSERINFO' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
What is the problem and how can I solve it? Olease can anyone help me?
You need to actually create the procedure, which you haven't done. You need a semi-colon after end and if you're creating it in SQL*Plus you need to add / to inform SQL*Plus that the block is finished:
create or replace procedure getUserInfo
( p_username out Users.username%TYPE,
p_password out Users.password%TYPE ) IS
BEGIN
select username,password into p_username,p_password from users;
END;
/
show error
It's always wise to add show error afterwards as well so that any errors are returned to the console in an understandable format.
Did you actually execute your create procedure statement? Did you get a "Procedure created." message? The fact that Oracle does not know of your getuserinfo procedure indicates to me that this statement was not performed.
I think it's procedure calling mistake!! Calling Should be like below:
SQL>var var1 varchar2(50);
SQL>var var2 varchar2(50);
SQL> exec getuserinfo(:var1, :var2);
SQL> print var1, var2;
Have Fun!!
You need to specify the out parameters when you call the procedure.
DECLARE
x Users.username%TYPE;
y Users.password%TYPE;
BEGIN
getuserinfo(x, y);
DBMS_OUTPUT.PUT_LINE('username: ' || x || ', password: ' || y);
END;