Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a cursor in SQL Developer to generate a subsequent query for each record

I have an SQL Developer query in which I would like to use a cursor to generate subsequent select statements.

But, I’m doing something wrong. I don’t seem to be using the cursor correctly.

Here is my code thus far. Thanks much.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DECLARE

  numMaterials  NUMBER := 0;

  CURSOR item_id_cur
      IS
       select id from item_h
       where value = 'myvalue';
                 
    l_item_id   item_id_cur%ROWTYPE;
BEGIN

  OPEN item_id_cur;

  LOOP
    FETCH item_id_cur INTO l_item_id;
    EXIT WHEN item_id_cur%NOTFOUND;

    SELECT count(*)
     INTO numMaterials
     FROM item_material_h
        WHERE fk_item = l_item_id;    --Does NOT like this line!!
    
    DBMS_OUTPUT.put_line (numMaterials);
   
  END LOOP; 
END;

>Solution :

You have to reference particular column, i.e.

WHERE fk_item = l_item_id.id;   

because of

CURSOR item_id_cur IS select id from item_h
                             --
                             this

Alternatively, you could have declared

l_item_id item_h.id%type;          --> note ".id%type"!

and then use it as you already did:

FETCH item_id_cur INTO l_item_id;
(...)       

WHERE fk_item = l_item_id;   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading