如何从pl / sql过程中从for循环返回ref_cursor

时间:2012-09-29 18:42:21

标签: stored-procedures plsql

我想找回下面sp的ref光标。但它没有工作..没有得到任何记录..所以请帮助我。

DATA_INPUTLIST - 它是两个变量的集合类型(mtn,mtnEffDate) DATA_RESULTLIST - 它也是3个变量的集合类型(id,effDate,mtn)

 CREATE OR REPLACE PROCEDURE proc1 (
       pCustId  IN  NUMBER,
       pAcctNo    IN    NUMBER,
       pSearchCriteria    IN    DATA_INPUTLIST,
       pRecordSet OUT SYS_REFCURSOR,
       out_error_code   OUT   NUMBER,
       out_error_message   OUT   VARCHAR2
    ) AS

        --Variables
        v_SUN_DATE DATE := TO_DATE('01/15/2011', 'mm/dd/yyyy');
        vCount    NUMBER := 0;
        v_mtn NUM

BER;
    v_mtn_eff_date DATE;
    cMtnPricePlanInfo DATA_RESULTLIST;

BEGIN

    SELECT COUNT(*) INTO vCount FROM TABLE (pSearchCriteria);

    FOR i IN 1..vCount LOOP

        SELECT MTN, TO_DATE(MTN_EFF_DATE, 'mm/dd/yyyy') into v_mtn, v_mtn_eff_date 
        FROM TABLE (pSearchCriteria) 
        WHERE 
        ROWNUM = i; 

        SELECT 
            A.PPLAN_ID, A.EFF_DATE, A.MTN INTO cMtnPricePlanInfo(i).PPLAN_ID, cMtnPricePlanInfo(i).EFF_DATE, cMtnPricePlanInfo(i).MTN
        FROM CUST_ACCT_LINE_PPLAN A, CUST_ACCT_LINE_PPLAN_HIST B
        WHERE
            A.CUST_ID  = pCustId
            AND A.ACCT_NO    = pAcctNo
            AND A.MTN = v_mtn
            AND A.MTN_EFF_DATE = v_mtn_eff_date
            AND A.EFF_DATE >= 
                (SELECT MAX(EFF_DATE) FROM CUST_ACCT_LINE_PPLAN_HIST C
                WHERE
                    C.CUST_ID  = pCustId
                    AND C.ACCT_NO    = pAcctNo
                    AND C.MTN = v_mtn
                    AND C.MTN_EFF_DATE = v_mtn_eff_date
                    AND C.EFF_DATE <= v_SUN_DATE)
            AND A.CUST_ID = B.CUST_ID
            AND A.ACCT_NO = B.ACCT_NO
            AND A.MTN = B.MTN
            AND A.MTN_EFF_DATE = B.MTN_EFF_DATE
            AND A.PPLAN_ID = B.PPLAN_ID;

    END LOOP;
       OPEN pRecordSet FOR
            SELECT * FROM TABLE (cMtnPricePlanInfo);

END;
/

1 个答案:

答案 0 :(得分:1)

对我来说最重要的事情如下:

    SELECT MTN, TO_DATE(MTN_EFF_DATE, 'mm/dd/yyyy') into v_mtn, v_mtn_eff_date 
    FROM TABLE (pSearchCriteria) 
    WHERE 
    ROWNUM = i; 

对于大于1的i,这不会返回任何内容。出于这个原因,请参阅this question。实际上,因为您使用SELECT ... INTO ...,如果no data found大于或等于2,您将收到i错误。

您不需要SQL查询来从PL / SQL集合中获取某些值。尝试用

替换此查询
    v_mtn := pSearchCriteria(i).MTN;
    v_mtn_eff_date := TO_DATE(pSearchCriteria(i).MTN_EFF_DATE, 'mm/dd/yyyy');

我没有你的表格,也没有数据,所以我不能确定为什么你没有数据。我只能冒一些明显的建议:vCount是零吗?如果为v_mtnv_mtn_eff_date添加合适的值并自行运行查询,与此存储过程分开,是否会返回任何行?