0

我有一个场景,一个有 for 循环的过程,其中有一个选择查询,每次运行时都会返回不同的结果。我必须将每个结果存储在单个变量或数组或游标中。请建议!!。下面提供的是写在一个包中的过程:

包装规格:

TYPE t_product_id IS TABLE OF products.productnr%TYPE INDEX BY PLS_INTEGER;

TYPE t_product_desc IS TABLE OF varchar(100) INDEX BY PLS_INTEGER;

Type t_product_psnr IS TABLE OF prodsubs.PSNR%TYPE INDEX BY PLS_INTEGER; TYPE t_prod_contractperiod IS TABLE OF prodrul.CONTRPEROFPSNR%TYPE INDEX BY PLS_INTEGER;

Type t_output IS TABLE OF prodrul.ALLOWEDCUSTTYPES%TYPE INDEX BY PLS_INTEGER;

包体:

PROCEDURE x_proc_Validprodcusttypeacctyp (
  i_prodnr        IN      t_product_id,
  i_invoiceprd    IN      prodrul.ALLOWEDINVPERIOD%TYPE,
  i_psnr          IN      t_product_psnr,
  i_contactprd    IN      t_prod_contractperiod,
  i_result        OUT     t_output

)

IS

BEGIN
  FOR i IN i_prodnr.FIRST .. i_prodnr.LAST

  `LOOP
     BEGIN
            select PRODUCTNR,PRODUCTUSERKEY,ALLOWEDCUSTTYPES,ALLOWEDACCNTTYPES 
            into  i_result (i)`
            from  prodrul rul  join products prods on rul.PRODUCTNROFPSNR = prods.productnr 
            where prods.productnr = i_prodnr (i)   and ALLOWEDINVPERIOD= i_invoiceprd and CONTRPEROFPSNR=  i_contactprd (i)
            and   prods.productnr not in(select productnr from products where SMARTCARDYN = 1)
            and   rul.PRODUCTNROFPSNR not in (select PSPRODUCTNR from prodsubs  join decoders on prodsubs.PSNR = decoders.DECSCPSNR where prodsubs.PSNR= i_psnr (i));
     END;
  END LOOP;


END x_proc_Validprodcusttypeacctyp;
4

1 回答 1

0

您可以将sys_refcursor其用作集合对象来返回多个列,而不是使用数组。例如

  CREATE OR REPLACE PROCEDURE x_proc_validprodcusttypeacctyp (
    i_prodnr        IN   t_product_id,
    i_invoiceprd    IN   prodrul.allowedinvperiod%TYPE,
    i_psnr          IN   t_product_psnr,
    i_contactprd    IN   t_prod_contractperiod,
    i_result            OUT t_output,
    o_cursor            OUT SYS_REFCURSOR
)
IS
BEGIN
    OPEN o_cursor FOR
        SELECT  productnr, productuserkey, allowedcusttypes, allowedaccnttypes
          FROM      prodrul rul
                    JOIN
                        products prods
                    ON rul.productnrofpsnr = prods.productnr
         WHERE       prods.productnr = i_prodnr
                    AND allowedinvperiod = i_invoiceprd
                    AND contrperofpsnr = i_contactprd
                    AND prods.productnr NOT IN (SELECT   productnr
                                                            FROM     products
                                                          WHERE  smartcardyn = 1)
                    AND rul.productnrofpsnr NOT IN
                             (SELECT   psproductnr
                                 FROM     prodsubs
                                          JOIN
                                              decoders
                                          ON prodsubs.psnr =
                                                  decoders.decscpsnr
                                WHERE   prodsubs.psnr = i_psnr);
END;
/
于 2013-08-22T10:13:57.747 回答