显示包含VARRAY列的记录

时间:2016-10-31 09:55:31

标签: plsql record varray

我有一个返回RECORD的函数。 其中一个记录栏是VARRAY。 有人可以提示我如何显示RECORD吗? (我的问题与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;

3 个答案:

答案 0 :(得分: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功能的期望。

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

答案 1 :(得分:0)

select * from table(SHOW_DETAILS.SHOW_CUSTOMER_DETAILS(101));

函数调用是:

PropTypes

答案 2 :(得分: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));