需要将输出作为select语句,而不是使用dbms_output

时间:2014-01-08 07:24:04

标签: oracle plsql sys-refcursor ref-cursor

我想在此过程中添加一个sys_refcursor作为out参数

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

/ *我想在此过程中添加一个sys_refcursor作为out参数* /

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

/ 声明参考光标的类型 /

TYPE V_RC IS REF CURSOR RETURN VRC;

/ *如何将此作为out参数与in参数一起使用。* /

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

/ *我想在结果集* /

中添加此变量作为列名
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 ';

/ *有没有办法将这个可变值添加到表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);

/ *而不是使用dbms_output,我需要从select语句* /

获取输出
  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;
/

1 个答案:

答案 0 :(得分:2)

走向天堂的步骤:

  1. 将程序更改为功能
  2. 创建一个oracle类型及其类型的集合
  3. 每次在函数中填充对象
  4. 设置函数类型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;
    
  5. 您可以稍后选择

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

    请参阅此处pipeline functions以了解流水线功能