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)
postgresql自定义类型并返回数组的更多相关文章
- webservice调用接口,接口返回数组类型
1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...
- WebApi 接口返回值不困惑:返回值类型详解。IHttpActionResult、void、HttpResponseMessage、自定义类型
首先声明,我还没有这么强大的功底,只是感觉博主写的很好,就做了一个复制,请别因为这个鄙视我,博主网址:http://www.cnblogs.com/landeanfen/p/5501487.html ...
- 【Spring】利用spring的JdbcTemplate查询返回结果映射到自定义类型
// org.springframework.jdbc.core.JdbcTemplate 中的查询方法基本都有支持参数RowMapper<T> rowMapper的重载方法.下面只是随便 ...
- 《精通C#》自定义类型转化-扩展方法-匿名类型-指针类型(11.3-11.6)
1.类型转化在C#中有很多,常用的是int类型转string等,这些都有微软给我们定义好的,我们需要的时候直接调用就是了,这是值类型中的转化,有时候我们还会需要类类型(包括结构struct)的转化,还 ...
- 用typedef自定义类型语法你真的会吗
关于typedef 我们学习typedef的时候,他的定义语法是:typedef+类型+别名,但是按照上面的格式,自定义数组怎么定义呢,是这样tepedef int a[10] 别名?还是这样tep ...
- 一个关于自定义类型作为HashMap的key的问题
在之前的项目需要用到以自定义类型作为HashMap的key,遇到一个问题:如果修改了已经存储在HashMap中的实例,会发生什么情况呢?用一段代码来试验: import java.util.HashM ...
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- struts2自定义类型转换器
首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...
- JavaScript之面向对象学习七(动态原型模式、寄生构造函数模式、稳妥构造函数模式创建自定义类型)
一.动态原型模式 在面向对象学习六中的随笔中,了解到组合构造函数模式和原型模式创建的自定义类型可能最完善的!但是人无完人,代码亦是如此! 有其他oo语言经验的开发人员在看到独立的构造函数和原型时,很可 ...
随机推荐
- Spring Cloud Gateway中异常处理
最近我们的项目在考虑使用Gateway,考虑使用Spring Cloud Gateway,发现网关的异常处理和spring boot 单体应用异常处理还是有很大区别的.让我们来回顾一下异常. 关于异常 ...
- 【原创】深入理解c++的右值引用
0 左值和右值 一个左值表达式代表的是对象本身,而右值表达式代表的是对象的值:变量也是左值. 1 右值引用作用 为了支持移动操作(包括移动构造函数和移动赋值函数),C++才引入了一种新的引 ...
- Centos7.5基于MySQL5.7的 InnoDB Cluster 多节点高可用集群环境部署记录
一. MySQL InnoDB Cluster 介绍MySQL的高可用架构无论是社区还是官方,一直在技术上进行探索,这么多年提出了多种解决方案,比如MMM, MHA, NDB Cluster, G ...
- CSS语法基础
引言:CSS语法 CSS规则由两个主要的部分构成:选择器,以及一条或者多条声明. selector { property: value; property: value; ... property: ...
- 通过修改hosts解决gist.github.com无法访问的问题
1.打开mac终端先ping一下 ping 192.30.253.119 如果能ping通的话 ,说明可以访问 2.修改hosts文件,添加如下语句: 192.30.253.118 gist.gith ...
- #21 Python异常
前言 运行程序时经常遇到各种错误,例如:ImportError(导入模块错误).IndexError(索引错误).NameError(变量错误).SyntaxError(语法错误).Indentati ...
- OJ:神秘的数组初始化
描述 填空,使得程序输出指定结果 #include <iostream> using namespace std; int main() { int * a[] = { // 在此处补充你 ...
- 未能加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0…错误问题的解决
1.分析原因: 出现未能加载文件或程序集 Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0的问题的原因是,Microsoft ...
- Tomcat日志设定
1 Tomcat 日志概述 Tomcat 日志信息分 为 两 类 : 一.是运行中的日志,它主要 记录 运行的一些信息,尤其是一些异常 错误 日志信息 .二.是 访问 日志信息,它 记录 的 访 ...
- JQuery官方学习资料(译):Attributes
Attributes 元素的属性可以为你的应用程序包含有用的信息,重要的是能够获取和设置它. .attr()方法 .attr()方法是可获取和可设置的,在设置状态下,.attr()可以 ...