copy with ref cursor

hi, i would like to copy some datas to a log table.
the procedure header is fix!



  TYPE T_RC IS REF CURSOR;


procedure load (vid IN NUMBER,vList OUT T_RC ) IS

how to declare a log_cursor?..

begin

 vQuery:='select username,password from profile where id=vid';

 open VList for vQuery;

for vlistrecord in vlist loop
      insert into log_cursos values (vlistrecord.username,vlistrecord.password);

 end loop;

end load;




sorry or bad English!!

ize-ez-jo-lesz

ize-ez-jo-leszAsked:
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.

andrewstCommented:
You cannot use a FOR loop with a REF CURSOR, you have to OPEN, FETCH and CLOSE explicitly.  Also, you must declare vlistrecord with the appropriate datatypes for the REF CURSOR, i.e. in this case (username,password).

Also, you need to treat vid as a bind variable in the vQuery string - e.g. :vid.  Then bind the vid value with the USING clause of the OPEN.

So your code will be something like:

procedure load (vid IN NUMBER,vList OUT T_RC ) IS
  TYPE vlist_t IS RECORD( username VARCHAR2(30),
                          password VARCHAR2(30) );
  vlistrecord vlist_t;
  vQuery VARCHAR2(200);
begin
  vQuery:='select username,password from profile where id=:vid';

  open VList for vQuery;
  loop
    fetch VList into vlistrecord using vid;
    exit when VList%notfound;
    insert into log_cursos(username,password)
    values (vlistrecord.username,vlistrecord.password);
  end loop;

end load;

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
stemu2000Commented:
here is the correct version:



PROCEDURE load (vid IN NUMBER,vList OUT T_RC ) IS
 vQuery VARCHAR2(200);
 v1  VARCHAR2(200);
 v2  VARCHAR2(200);
BEGIN
 vQuery:='select username,password from profile where id=:vid';

 OPEN VList FOR vQuery USING vid;
 LOOP
   FETCH VList INTO v1,v2;
   EXIT WHEN VList%NOTFOUND;
--   INSERT INTO log_cursos(username,password)
 --  VALUES (v1,v2);
 END LOOP;
 CLOSE VList;
 
 OPEN VList FOR vQuery USING vid;

END load;
/


make sure you ppass bind parameters in the OPEN call, not the FETCH call! - and also to pass back the original cursor after logging it, it is acctually necessary to reopen the cursor!


Cheers, Stefan

0
stemu2000Commented:
ups, forgot to remove the commented out code for inserting:


PROCEDURE load (vid IN NUMBER,vList OUT T_RC ) IS
vQuery VARCHAR2(200);
v1  VARCHAR2(200);
v2  VARCHAR2(200);
BEGIN
vQuery:='select username,password from profile where id=:vid';

OPEN VList FOR vQuery USING vid;
LOOP
  FETCH VList INTO v1,v2;
  EXIT WHEN VList%NOTFOUND;
  INSERT INTO log_cursos(username,password)
  VALUES (v1,v2);
END LOOP;
CLOSE VList;

OPEN VList FOR vQuery USING vid;

END load;
/


Cheers, Stefan
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.