Postgresql:自定义类型+数组组合

时间:2018-04-30 05:50:16

标签: arrays postgresql types casting postgresql-9.4

我无法找到以下程序的问题所在。当我在最后使用RAISE NOTICE语句时,自定义类型的值显示没有任何错误。当我运行最后的select语句时,错误是Array value must start with "{" or dimension information.请帮我看一下如何调用包/函数的select语句。

create
or
replace TYPE t_col_foo as object
(
    ID            NUMBER
    , CLUSTERNAME VARCHAR2(300)
    , "1200AM"    varchar2(10));

create
or
replace TYPE T_COL_R AS TABLE OF t_col_foo;

CREATE OR REPLACE PACKAGE foo_avail_pkg
IS


FUNCTION foo_slots
                  (
                      p_ref_data anyarray
                  )
    RETURN t_col_r[];
END foo_avail_pkg;

CREATE OR REPLACE PACKAGE BODY foo_avail_pkg
IS
FUNCTION foo_slots
                  (
                      p_ref_data anyarray
                  )
    RETURN t_col_r[]
IS

    --  declare
    r_target_data t_col_foo:=t_col_foo(null,null,null);
    r_target_data_1 t_col_foo;
    r_source_data text[];
    t_return t_col_tab1;

BEGIN
    t_return:=t_col_tab1();

    select
           array
           (
                  select
                         unnest( p_ref_data )
           )
    into   r_source_data
    ;

    --  r_target_data = '{}';
    for i in coalesce(array_lower(r_source_data,1),0) .. coalesce(array_upper(r_source_data,1),0)
    LOOP

        r_target_data.ID          := substr(r_source_data[i],1,instr(r_source_data[i],',',1,1)-1);
        r_target_data.CLUSTERNAME := substr(r_source_data[i],length(r_target_data.ID)+2,(instr(r_source_data[i],',',length(r_target_data.ID)+1,2) - instr(r_source_data[i],',',1,1))-1);
        r_target_data."1200AM"    := 3;

        r_target_data_1 :=row(r_target_data.ID ,r_target_data.CLUSTERNAME,r_target_data."1200AM") :: t_col_foo;
    END LOOP;
    --  dbms_output.put_line(r_target_data_1);
    RETURN r_target_data_1;
end;

END foo_avail_pkg;

这就是我必须打电话的方式

 select * from foo_avail_pkg.foo_SLOTS(array
                             (
                                    select
                                           ID
                                                  ||','
                                                  ||CLUSTER_NAME   
||','
                                              ||LOB                                           
                                    from
                                           y limit 1
                             ));

错误是

ERROR:  malformed array literal: "(1398,Sanity20feb,3)"
DETAIL:  Array value must start with "{" or dimension information.                     

0 个答案:

没有答案