Question

i want to add a sys_refcursor as out parameter in this procedure

CREATE OR REPLACE PROCEDURE APIN
(
ADT APT.A_DATE%TYPE,
DID APT.D_ID%TYPE
)

/* i want to add a sys_refcursor as out parameter in this procedure */

AS
TYPE VRC IS RECORD
(
V_DT DATE,
V_ID NUMBER
);

/declaring the type for ref cursor/

TYPE V_RC IS REF CURSOR RETURN VRC;

/* how can i use this as out parameter along with the in parameters.*/

R V_RC;
RC SYS_REFCURSOR;
V_CNT NUMBER := 0;
V_STATUS VARCHAR2(30); 

/* i want to add this variable as column name in the result set*/

V_ST_DT DATE; 
V_ED_DT DATE;
BEGIN
V_ST_DT := TRUNC(TO_DATE(ADT,'DD/MM/YY'),'MM'); 
/*getting first date of the month*/
V_ED_DT := LAST_DAY(V_ST_DT); 
/*getting last date of the month*/
LOOP
OPEN RC FOR 
/* selecting v_cnt value using ref cursor*/
SELECT MAX(RN) FROM (SELECT ROW_NUMBER()OVER(ORDER BY P_ID) RN FROM APT
WHERE D_ID = DID
AND A_DATE = V_ST_DT);
FETCH RC INTO V_CNT; 
  IF V_CNT >= 10 THEN
    V_STATUS := ' NOT AVAILABLE ON ';

/* is there any way to add this vairable values into the column of the table apt?*/

 OPEN R FOR
    SELECT A_DATE, D_ID FROM APT
    WHERE D_ID = DID
    AND A_DATE = V_ST_DT;
    DBMS_OUTPUT.PUT_LINE(DID||V_STATUS||V_ST_DT);

/*instead of using dbms_output, i need to get output from select statement*/

  ELSE 
    V_STATUS := ' AVAILABLE ON ';
    OPEN R FOR
    SELECT A_DATE, D_ID FROM APT
    WHERE D_ID = DID
    AND A_DATE = V_ST_DT;
    DBMS_OUTPUT.PUT_LINE(DID||V_STATUS||V_ST_DT);
/*instead of using dbms_output, i need to get output from select statement*/
  END IF;
  V_ST_DT := V_ST_DT + 1; 
/*incrementing date variable to check next date*/
  EXIT WHEN V_ST_DT > V_ED_DT;
END LOOP;
CLOSE RC; 
/*closing the ref cursor*/
END;
/
Was it helpful?

Solution

Steps to heaven:

  1. change procedure to function
  2. create an oracle type and collection of its type
  3. fill the object every time in function
  4. set the function type pipelined

    create type my_output as object(mycol1 number,mycol2 varchar2(200));
    
    create type my_collection as table of my_output;
    
    create function apin(...) return my_collection pipelined;
    

You can select it later as

select * from table(cast(apin as <yourcollectionname>));

See here pipeline functions to learn about pipelined functions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top