Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch query result into cursor within procedure

I'm migrating many procedures from MySQL to oracle and I've found some issues. This is what I've got so far:

CREATE OR REPLACE PROCEDURE PROCEDURE1(PROCEDURE_PARAM IN NUMBER)
IS
CURSOR C1 (CURSOR_PARAMETER IN NUMBER)
IS
 SELECT COL1, COL2, MY_FUNCTION1(P1), SUM(COL3), MY_FUNCTION2(P1,P2)
        COL4, MY_FUNCTION(P1,P2,P3,P4)
 FROM TABLE_1
 WHERE COL1 = CURSOR_PARAMETER -- How to reference it to 'Procedure_param' ?
 GROUP BY COL1, COL2, COL4
 ORDER BY COL2;

BEGIN
    -- HOW TO FETCH RESULTS INTO CURSOR ??
END PROCEDURE1;
/

Those are an example of name columns and functions(user created functions). My concerns are how to tell the cursor that the parameter CURSOR_PARAMETER is the same of PROCEDURE_PARAM and also the most important, how to fetch the results into the cursor? I'm stuck in this problem. Any help would be appreciated. With this example I could start converting all procedures I have to.

Best regards.

like image 565
Alejandro Bastidas Avatar asked Apr 28 '26 23:04

Alejandro Bastidas


1 Answers

you'd fetch from the cursor like :

BEGIN
  for r_row in C1(PROCEDURE_PARAM)
  loop
    dbms_output.put_line(r_row.col1);--or whatever you want to do here.
  end loop;
end;

though i usually prefer to write these inline as

begin
  for r_row in (SELECT COL1, COL2, MY_FUNCTION1(P1), 
                       SUM(COL3), MY_FUNCTION2(P1,P2)
                       COL4, MY_FUNCTION(P1,P2,P3,P4)
                  FROM TABLE_1
                 WHERE COL1 = PROCEDURE_PARAM 
                 GROUP BY COL1, COL2, COL4
                 ORDER BY COL2)
  loop
    ...
  end loop;

just so I don't have to scroll up to a cursor def to see what it was actually doing.

like image 92
DazzaL Avatar answered May 01 '26 12:05

DazzaL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!