zoukankan      html  css  js  c++  java
  • postgresql自定义类型并返回数组

    转自 https://blog.csdn.net/victor_ww/article/details/44415895

    create type custom_data_type as (
    id int,
    name varchar(50),
    score decimal(5,2),
    create_time timestamp
    );
     
    create or replace function custom_data_type_demo(p_order_unit_array varchar[],p_goods_array int[])
    returns custom_data_type[] as $$
    declare
    v_order_unit_array varchar[] := array['a','b','c']::varchar[];
    v_goods_array int[] := array[60.56,82.12,95.32]::int[];
    v_tmp_result custom_data_type;
    v_result_array custom_data_type[];
    v_index int := 0;
    v_order varchar(100);
    v_goods int;
    begin
    if p_order_unit_array is not null then
    v_order_unit_array := p_order_unit_array;
    end if;
     
    if p_goods_array is not null then
    v_goods_array := p_goods_array;
    end if;
     
    raise notice '-------1---------';
    <<order_label>> foreach v_order in array v_order_unit_array loop
    <<goods_label>> foreach v_goods in array v_goods_array loop
    v_tmp_result.id = v_index*round(random()*10);
    v_tmp_result.name = v_order;
    v_tmp_result.score = v_goods;
    v_tmp_result.create_time = current_timestamp;
        raise notice '-------goods_label---------';
    end loop goods_label;
    raise notice '-------order_label---------v_index';
    v_result_array[v_index] = v_tmp_result;
    v_index := v_index + 1;
    end loop order_label;
    raise notice '-------2---------';
    return v_result_array;
    exception when others then
    raise exception 'error happen(%)',sqlerrm;
    end;
    $$ language plpgsql;
     
    select custom_data_type_demo(null,null);
    exampledb=> select custom_data_type_demo(null,null);
                                                                                        custom_data_type_demo
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     [0:3]={"(0,a,95.00,"2018-10-31 17:43:53.836608")","(1,b,95.00,"2018-10-31 17:43:53.836608")","(2,c,95.00,"2018-10-31 17:43:53.836608")","(3,d,95.00,"2018-10-31 17:43:53.836608")"}
    (1 row)
    
    解析数组,可用于不定参数时,可使用传数组的方式,类似于SQL Server 的Xml;
    exampledb=> select T from unnest(custom_data_type_demo(null,null)) as T; t ------------------------------------------ (0,a,95.00,"2018-10-31 17:35:40.881777") (1,b,95.00,"2018-10-31 17:35:40.881777") (2,c,95.00,"2018-10-31 17:35:40.881777") (3,d,95.00,"2018-10-31 17:35:40.881777") (4 rows) exampledb=> select T.id,T.name,T.score,T.create_time from unnest(custom_data_type_demo(null,null)) as T; id | name | score | create_time ----+------+-------+---------------------------- 0 | a | 95.00 | 2018-10-31 17:40:25.939054 1 | b | 95.00 | 2018-10-31 17:40:25.939054 2 | c | 95.00 | 2018-10-31 17:40:25.939054 3 | d | 95.00 | 2018-10-31 17:40:25.939054 (4 rows)
  • 相关阅读:
    表表达式,Substring, CharIndex, 多行数据变同一行的用法
    武汉三首,记录备忘,写的不好,以后再改
    竟然又有两年没有码字发帖了,真是快长草了,打磨一下,克服拖延症,重新回归,重新写起!
    屈指一算,竟然有一年半没有发帖了,真是时光荏苒,白云苍狗!
    下雨有感
    (ETW) Event Trace for Windows 提高 (含pdf下载)
    (ETW) Event Tracing for Windows 入门 (含pdf下载)
    Requirejs加载超时问题的一个解决方法:设置waitSeconds=0
    如何通过Socket TCP发送并接收一个文件?
    Microsoft.VisualBasic.DateAndTime.Timer 与 DateTime.Now.TimeOfDay.TotalSeconds 相当
  • 原文地址:https://www.cnblogs.com/Richard2014/p/9930636.html
Copyright © 2011-2022 走看看