0

我有一个返回记录的函数。记录的列之一是 VARRAY。有人可以提示我如何显示记录吗?(我的问题与 VARRAY 列有关。

create or replace TYPE phone_list_typ AS VARRAY(5) OF VARCHAR2(25);

CREATE TABLE "CUSTOMERS" 
    ("CUSTOMER_ID" NUMBER(6,0), 
     "CUST_FIRST_NAME" VARCHAR2(20 BYTE)
     "PHONE_NUMBERS" "OE"."PHONE_LIST_TYP" , 
     "CREDIT_LIMIT" NUMBER(9,2), 
     "CUST_EMAIL" VARCHAR2(40 BYTE)); 

TYPE r_cust_det IS RECORD( CUSTOMER_ID      customers.CUSTOMER_ID%TYPE
                         , CUST_FIRST_NAME  customers.CUST_FIRST_NAME%TYPE
                         , PHONE_NUMBERS    customers.PHONE_NUMBERS%TYPE
                         , CREDIT_LIMIT     customers.CREDIT_LIMIT%TYPE
                         , CUST_EMAIL       customers.CUST_EMAIL%TYPE);

CREATE OR REPLACE FUNCTION show_customer_details (n_customer_id customers.customer_id%TYPE) RETURN r_cust_det
IS
    v_return r_cust_det;
BEGIN
    SELECT CUSTOMER_ID
         , CUST_FIRST_NAME
         , PHONE_NUMBERS
         , CREDIT_LIMIT
         , CUST_EMAIL
    INTO v_return
    FROM CUSTOMERS
    WHERE CUSTOMER_ID = n_customer_id;
RETURN v_return;
END show_customer_details;
4

3 回答 3

2

这可能取决于您希望它的外观和显示媒体是什么(文本文件、交互式网页等),但一种方法可能是将电话号码列为逗号分隔的列表。

select customer_id, cust_first_name, credit_limit, cust_email
     , listagg(p.column_value,', ') within group (order by p.column_value) as phone_numbers
from   customers c cross join table(c.phone_numbers) p
group by customer_id, cust_first_name, credit_limit, cust_email
order by customer_id;

不过,我不确定您对show_customer_details功能的期望。

(顺便说一句,除非绝对必须,否则将标识符括在双引号中并不是一个好主意。)

于 2016-10-31T10:10:42.577 回答
0
CREATE OR REPLACE FUNCTION show_customer_details (n_customer_id customers.customer_id%TYPE) RETURN t_cust_det PIPELINED
    IS
        v_return t_cust_det;
    BEGIN
        SELECT t1.CUSTOMER_ID
             , t1.CUST_FIRST_NAME
             , t2.*
             , t1.CREDIT_LIMIT
             , t1.CUST_EMAIL
        BULK COLLECT INTO v_return
        FROM CUSTOMERS t1, table(t1.phone_numbers) t2
        WHERE t1.CUSTOMER_ID = n_customer_id
        AND column_value is not null;

        FOR i IN 1 .. v_return.count
        LOOP
            PIPE ROW (v_return(i));
        END LOOP;

    END show_customer_details;

函数调用是:

select * from table(SHOW_DETAILS.SHOW_CUSTOMER_DETAILS(101));
于 2016-10-31T10:43:55.483 回答
0

我发现的另一个解决方案,不使用 PIPELINED 是:

定义对象类型

create or replace type customers_typ
is object 
    ( CUSTOMER_ID      number(6)
    , CUST_FIRST_NAME  varchar2(20)
    , PHONE_NUMBERS    varchar2(25) --phone_list_typ
    , CREDIT_LIMIT     number(9, 2)
    , CUST_EMAIL       varchar2(40)
    );

定义一个新类型,即先前定义对象的表。

create or replace type t_customers_typ is table of customers_typ;

函数变成

CREATE OR REPLACE FUNCTION show_customer_details (n_customer_id customers.customer_id%TYPE) RETURN t_customers_typ
IS
    v_return t_customers_typ;
BEGIN
    SELECT customers_typ(t1.CUSTOMER_ID
         , t1.CUST_FIRST_NAME
         , t2.column_value
         , t1.CREDIT_LIMIT
         , t1.CUST_EMAIL)
    BULK COLLECT INTO v_return
    FROM CUSTOMERS t1, table(t1.phone_numbers) t2
    WHERE t1.CUSTOMER_ID = n_customer_id
    AND t2.column_value is not null;

    return v_return;

END show_customer_details;

该函数的调用方式相同:

select * from table(SHOW_DETAILS.SHOW_CUSTOMER_DETAILS(101));
于 2016-10-31T15:17:28.113 回答