使用游标循环访问Pivot查询

时间:2019-03-05 01:41:01

标签: oracle plsql oracle12c

DECLARE    
     CURSOR curresult IS 
         SELECT *
         FROM (
              SELECT pa.attribute_bill_name,
                     cpad.attribute_value
              FROM custproductattrdetails cpad,
                   productattribute pa
              WHERE cpad.product_id = pa.product_id 
              AND cpad.customer_ref = p_customerref
              AND cpad.product_seq = p_productseq 
              AND cpad.start_dat <= p_terminationdate
              AND (
                   cpad.end_dat IS NULL 
                   OR cpad.end_dat >= p_terminationdate
              ) 
              AND cpad.product_attribute_subid = pa.product_attribute_subid 
              AND pa.attribute_bill_name IN (
                   'BILLED_INSTALLMENT_NUMBER',
                   'TOTAL_INSTALLMENT_NUMBER'
              )
         )
              PIVOT ( MAX ( attribute_value )
                   FOR attribute_bill_name
                   IN ( 'BILLED_INSTALLMENT_NUMBER' AS billed_installment_number,'TOTAL_INSTALLMENT_NUMBER'
                   AS total_installment_number )
              );
BEGIN
     FOR recresult IN curresult LOOP
          dbms_output.put_line(recresult.billed_installment_number || ' installment');
          IF dtchngpkgdate > dtnxtbilldate AND dtchngpkgdate > dtbilledtodate
          THEN
               p_billed_installments := recresult.billed_installment_number + 1;
          END IF;
          IF dtchngpkgdate < dtnxtbilldate AND dtchngpkgdate <= dtbilledtodate
          THEN
               p_billed_installments := recresult.billed_installment_number;
          END IF;
     END LOOP;    
END;

当我明确执行查询时,我得到了预期的结果。但是,当我尝试使用CURSOR For循环进行访问时,我收到的RECRESULT.BILLED_INSTALLMENT_NUMBER和RECRESULT.TOTAL_INSTALLMENT_NUMBER为NULL。 有人可以建议吗?

0 个答案:

没有答案