postgresql自定义类型并返回数组

前端之家收集整理的这篇文章主要介绍了postgresql自定义类型并返回数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. create type custom_data_type as (
  2. id int,name varchar(50),score decimal(5,2),create_time timestamp
  3. );
  4.  
  5. create or replace function custom_data_type_demo(p_order_unit_array varchar[],p_goods_array int[])
  6. returns custom_data_type[] as $$
  7. declare
  8. v_order_unit_array varchar[] := array['a','b','c']::varchar[];
  9. v_goods_array int[] := array[60.56,82.12,95.32]::int[];
  10. v_tmp_result custom_data_type;
  11. v_result_array custom_data_type[];
  12. v_index int := 0;
  13. v_order varchar(100);
  14. v_goods int;
  15. begin
  16. if p_order_unit_array is not null then
  17. v_order_unit_array := p_order_unit_array;
  18. end if;
  19.  
  20. if p_goods_array is not null then
  21. v_goods_array := p_goods_array;
  22. end if;
  23.  
  24. raise notice '-------1---------';
  25. <<order_label>> foreach v_order in array v_order_unit_array loop
  26. <<goods_label>> foreach v_goods in array v_goods_array loop
  27. v_tmp_result.id = v_index*round(random()*10);
  28. v_tmp_result.name = v_order;
  29. v_tmp_result.score = v_goods;
  30. v_tmp_result.create_time = current_timestamp;
  31. end loop goods_label;
  32. raise notice '-------a---------';
  33. v_result_array[v_index] = v_tmp_result;
  34. v_index := v_index + 1;
  35. end loop order_label;
  36. raise notice '-------2---------';
  37. return v_result_array;
  38. exception when others then
  39. raise exception 'error happen(%)',sqlerrm;
  40. end;
  41. $$ language plpgsql;
  42.  
  43. select custom_data_type_demo(null,null);

猜你在找的Postgre SQL相关文章