pass parameter to procedure to use in cursor

Hello experts,
I would need to pass a parameter value to a cursor in a pl/sql procedure.

create or replace procedure test1
cursor c as select * from emp where emp_name = 'test'

instead of above can we do it this way ? I am getting an error with this approach can you please help with the correct syntax in doing or give me an example please.
create or replace procedure test1 (p_empname in varchar2(20))
cursor c as select * from emp where emp_name = p_empname

Thank you!
newbieexpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
use "IS" for cursors, not "AS"

also, don't include a length specification on the parameter data type.

if you need to ensure it's only 20 characters or less, then check the length yourself inside the procedure.


CREATE OR REPLACE PROCEDURE test1(p_empname IN VARCHAR2)
IS
    CURSOR c
    IS
        SELECT *
          FROM emp
         WHERE emp_name = p_empname;
BEGIN
    OPEN c;
    --- do something with your cursor here
END;
0
slightwv (䄆 Netminder) Commented:
What you have is almost correct.  You don't specify the length of the parameter:
create or replace procedure myproc(p_name in varchar2) as
cursor mycur is select count(*) from dual where dummy=p_name;
junk number;
begin
	open mycur;
	fetch mycur into junk;
	close mycur;
	dbms_output.put_line('Got: ' || junk);
end;
/

show errors

exec myproc('X');

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbieexpertAuthor Commented:
Thank you very much!!!
0
Mark GeerlingsDatabase AdministratorCommented:
I'm curious, what will you use a procedure like this for?  Usually it is not a good idea to write cursors like:
select * from ... [a table]
since if the table definition ever changes, your procedure will need to be re-compiled (and/or changed, then recompiled).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.