如何从PLSQL函数返回记录表?

时间:2016-01-27 06:30:39

标签: oracle collections plsql

我有一个有三个不同查询的函数。第一个查询返回plsql记录类型中的单个记录。第二个查询返回另一个单值,第三个也返回不同的单个值。现在我想用这两个值附加第一条记录,并从我的函数返回该新记录的表。我怎样才能做到这一点。

create or replace function test(p_actBillDat date) return <what should I    return> as
 type tab_accountNum is table of account.account_num%type;
 var_accountNum tab_accountNum;

 type query1Record is record(
 accountNum account.account_num%type,
 customerRef customer.customer_ref%type,
 internalCreditScore CUSTOMERATTRIBUTES.Internal_Credit_Score%type);

 var_query1Rec query1Record;

 var_nsfDat date;
 var_writeOffdat date;



 cursor cur_accountNum is
 select ACCOUNT_NUM
  from BILLSUMMARY
 where trunc(ACTUAL_BILL_DTM) = p_actBillDat
   and CANCELLATION_REQUEST_DAT is null;
begin
  open cur_accountNum;
  Loop
    fetch cur_accountNum bulk collect
  into var_accountNum limit 100;
close cur_accountNum;
for i in 1 .. var_accountNum.count
loop 
  select A.ACCOUNT_NUM, A.CUSTOMER_REF, CA.INTERNAL_CREDIT_SCORE 
    into var_query1Rec
    from ACCOUNT A, CUSTOMERATTRIBUTES CA, CONTACTDETAILS CD, CONTACT CNT  
            where A.ACCOUNT_NUM = var_accountNum(i) and  
            A.CUSTOMER_REF = CA.CUSTOMER_REF(+) and  
            A.CUSTOMER_REF = CD.CUSTOMER_REF and  
            CNT.CUSTOMER_REF = A.CUSTOMER_REF and  
            CD.CONTACT_SEQ = CNT.CONTACT_SEQ and  
            CD.CONTACT_SEQ = 1 and  
            CD.START_DAT = (select min(CNTD.START_DAT)  
                            from CONTACTDETAILS CNTD  
                            where CNTD.CONTACT_SEQ = CD.CONTACT_SEQ and  
                            CNTD.CUSTOMER_REF = A.CUSTOMER_REF);

  select max(AP.ACCOUNT_PAYMENT_DAT) into var_writeOffdat 
            from ACCOUNT A, ACCOUNTPAYMENT AP  
            where A.ACCOUNT_NUM = AP.ACCOUNT_NUM and  
            A.ACCOUNT_NUM = var_accountNum(i) AND  
            A.TOTAL_WRITEOFF_TOT <> 0 and  
            (AP.PAYMENT_ORIGIN_ID = 2 or  AP.PAYMENT_ORIGIN_ID = 3) and  
            AP.CANCELLED_DTM is null and  
            AP.FAILED_DTM is null;

  select max(PP.FAILED_DTM) into var_nsfDat

                              from ACCOUNTPAYMENT AP, PHYSICALPAYMENT PP  
                              where AP.ACCOUNT_NUM = var_accountNum(i) and   
                              AP.ACCOUNT_PAYMENT_STATUS = 3 and  
                              AP.PHYSICAL_PAYMENT_SEQ = PP.PHYSICAL_PAYMENT_SEQ and  
                              AP.CUSTOMER_REF = PP.CUSTOMER_REF and  
                              PP.PHYSICAL_PAYMENT_STATUS = 3 and  
                              PP.FAILURE_CODE_ID in (select PFC.FAILURE_CODE  
                                                     from CGPAYMENTFAILURECONFIG PFC  
                                                     where PFC.FAILURE_TYPE = 'Decline NSF')  ;

 <how to appned var_query1Rec with  var_writeOffdat and var_writeOffdat>                                 
 <how to make a PLSQl table of that record and return from function>



  end loop;
end loop;

end;

1 个答案:

答案 0 :(得分:1)

如果此功能不是套餐的一部分 - 为什么它不会?那么你别无选择,只能像这个例子那样声明一个SQL Object类型:

CREATE TYPE person_typ AS OBJECT (
  idno           NUMBER,
 first_name     VARCHAR2(20)
);

声明函数顶部的变量以访问创建的类型。

type t_arr is table of person_typ ;
l_arr  t_arr := t_arr();

然后在您的代码中分配它们:

l_arr.extend;
l_arr(i).idno := xxx;
l_arr(i).first_name := yyyy;

create函数返回对象:

create or replace function test(p_actBillDat date) return person_typ as
.....
    return(l_arr);
end;
  

但是我会在包中然后在包体标题或规范中有这个功能,你可以这样做:

type t_rec is
 record(x number
       ,y varchar2(100)
       );
 type t_tbl is table of t_rec index by binary_integer;

然后在你的函数中声明:

 l_tbl t_tbl;

然后在函数中分配它们:

l_tbl(i).x := xxx;
l_tbl(i).y := yyy;

最后只需返回函数中的类型:

create or replace function test(p_actBillDat date) return t_tbl as
......
   l_tbl t_tbl;
begin
    .......
     for i in 1..counter loop
         .. SQL statements
         l_tbl(i).x := xxx;
         l_tbl(i).y := yyy;
     end loop;
     return l_tbl;
end;