Gumowy Kaczak
Gumowy Kaczak

Reputation: 1499

PL/SQL Fetch cursor into cursor

I have package in which I have declared cursor. And I want to return that cursor as OUT variable of PROCEDURE. I can't just get data from that cursor cause data needs to be loaded first by load_data proc. I wan't to use that MMM_CURSOR cause there's additional functionality, which uses that same select - writing to files at DB level.

create or replace PACKAGE BODY PCG_MMM AS

CURSOR MMM_CURSOR(OFFSET NUMBER) IS 
SELECT TYPE, VALUE FROM MMM_TEMP_LOGS WHERE VALUE = OFFSET;

TYPE RET_TYPE IS REF CURSOR;

PROCEDURE LOAD_DATA AS 
-- loading data into MMM_TEMP_LOGS--
END LOAD_DATA;

PROCDURE WRITE_TO_FILE AS
BEGIN
-- writing to file here --
END WRITE_TO_FILE;

PROCEDURE GET_DATA(DATA RET_TYPE, OFFSET NUMBER:=0) AS
BEGIN
   LOAD_DATA;

   DATA := MMM_CURSOR(OFFSET) ; <--- ??

END GET_DATA;

END PCG_MMM;

Upvotes: 0

Views: 1564

Answers (1)

DazzaL
DazzaL

Reputation: 21973

you can't pass a CURSOR out of a procedure, but you can pass a ref cursor. you also can't just convert a cursor to a ref cursor.

so if you want to use that cursor in a few places + have the ability to pass it out, then i'd recommend you change it to a view, and use that in lieu of a cursor.

eg

create view MMM_VIEW
as
SELECT TYPE, VALUE FROM MMM_TEMP_LOGS;

(if you were passing in variables within it and need to keep it that way for performance, look up "parameterised views")

create or replace PACKAGE BODY PCG_MMM AS

TYPE RET_TYPE IS REF CURSOR;

PROCEDURE LOAD_DATA AS 
-- loading data into MMM_TEMP_LOGS--
  for r_cur in (select * from mmm_view) loop...or whatever (replace the cursor with the view)
END LOAD_DATA;

PROCDURE WRITE_TO_FILE AS
BEGIN
-- writing to file here --
END WRITE_TO_FILE;

PROCEDURE GET_DATA(DATA out RET_TYPE, OFFSET NUMBER:=0) AS
BEGIN
   LOAD_DATA;

   open DATA for select * From mmm_view where value = OFFSET;

END GET_DATA;

END PCG_MMM;

Upvotes: 2

Related Questions