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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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.

    CURSOR c
        SELECT *
          FROM emp
         WHERE emp_name = p_empname;
    OPEN c;
    --- do something with your cursor here
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;
	open mycur;
	fetch mycur into junk;
	close mycur;
	dbms_output.put_line('Got: ' || junk);

show errors

exec myproc('X');

Open in new window

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!!!
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).
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.