Oracle vs PostgreSQL Develop(19) - PIPE ROW
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,Oracle的PL/SQL提供了Pipelined Table Functions特性用于把多行数据返回到调用者,可以有效的提升性能。在PostgreSQL中,可以通过在函数中利用SETOF或者RET
千家信息网最后更新 2025年11月07日Oracle vs PostgreSQL Develop(19) - PIPE ROW
Oracle的PL/SQL提供了Pipelined Table Functions特性用于把多行数据返回到调用者,可以有效的提升性能。
在PostgreSQL中,可以通过在函数中利用SETOF或者RETURN NEXT来实现。
Oracle
创建数据表,插入数据
TEST-orcl@DESKTOP-V430TU3>drop table t_piperow;drop table t_piperow *ERROR at line 1:ORA-00942: table or view does not existTEST-orcl@DESKTOP-V430TU3>create table t_piperow(id int,c1 timestamp ,c2 varchar2(20),c3 number);Table created.TEST-orcl@DESKTOP-V430TU3>TEST-orcl@DESKTOP-V430TU3>insert into t_piperow(id,c1,c2,c3) 2 select rownum,sysdate,'test'||rownum,123455.55 from dba_objects where rownum <= 500;500 rows created.TEST-orcl@DESKTOP-V430TU3>commit;Commit complete.TEST-orcl@DESKTOP-V430TU3>创建Type
TEST-orcl@DESKTOP-V430TU3>CREATE OR REPLACE TYPE rec_t_piperow AS OBJECT(id int,c1 timestamp ,c2 varchar2(20),c3 number); 2 /Type created.TEST-orcl@DESKTOP-V430TU3>CREATE OR REPLACE TYPE type_t_piperow AS TABLE OF rec_t_piperow; 2 /Type created.函数实现
TEST-orcl@DESKTOP-V430TU3>CREATE OR REPLACE FUNCTION func_piperow_demo1 RETURN type_t_piperow PIPELINED IS 2 BEGIN 3 FOR rec in (select * from t_piperow) LOOP 4 PIPE ROW (rec_t_piperow(rec.id,rec.c1,rec.c2,rec.c3)); 5 END LOOP; 6 RETURN; 7 END; 8 /Function created.查询数据
TEST-orcl@DESKTOP-V430TU3>column c3 format 9999999999999.9999TEST-orcl@DESKTOP-V430TU3>select * from table(func_piperow_demo1()) where rownum < 5; ID C1 C2 C3---------- -------------------- -------------------- ------------------- 1 31-OCT-19 10.50.38.0 test1 123455.5500 00000 AM 2 31-OCT-19 10.50.38.0 test2 123455.5500 00000 AM 3 31-OCT-19 10.50.38.0 test3 123455.5500 00000 AM 4 31-OCT-19 10.50.38.0 test4 123455.5500 00000 AMPostgreSQL
下面来看看PG的实现,创建表,插入数据
[local]:5432 pg12@testdb=# drop table if exists t_piperow;DROP TABLETime: 5.255 ms[local]:5432 pg12@testdb=# create table t_piperow(id int,c1 timestamp ,c2 varchar(20),c3 float);CREATE TABLETime: 4.711 ms[local]:5432 pg12@testdb=# [local]:5432 pg12@testdb=# insert into t_piperow(id,c1,c2,c3) pg12@testdb-# select x,now(),'test'||x,123455.55 from generate_series(1,500) x;INSERT 0 500Time: 10.183 ms[local]:5432 pg12@testdb=# [local]:5432 pg12@testdb=#函数实现
第一种方式,使用SQL:
[local]:5432 pg12@testdb=# CREATE OR REPLACE FUNCTION func_piperow_demo1() RETURNS SETOF PUBLIC.t_piperowpg12@testdb-# ASpg12@testdb-# $$pg12@testdb$# SELECT * FROM t_piperow;pg12@testdb$# $$pg12@testdb-# LANGUAGE SQL;CREATE FUNCTIONTime: 1.341 ms[local]:5432 pg12@testdb=# [local]:5432 pg12@testdb=# select func_piperow_demo1() limit 5; func_piperow_demo1 -------------------------------------------------- (1,"2019-10-31 11:09:27.222996",test1,123455.55) (2,"2019-10-31 11:09:27.222996",test2,123455.55) (3,"2019-10-31 11:09:27.222996",test3,123455.55) (4,"2019-10-31 11:09:27.222996",test4,123455.55) (5,"2019-10-31 11:09:27.222996",test5,123455.55)(5 rows)Time: 1.038 ms[local]:5432 pg12@testdb=#第二种方式,使用PL/pgSQL,RETURN QUERY
[local]:5432 pg12@testdb=# CREATE OR REPLACE FUNCTION func_piperow_demo2() RETURNS SETOF PUBLIC.t_piperowpg12@testdb-# ASpg12@testdb-# $$pg12@testdb$# BEGINpg12@testdb$# RETURN QUERY SELECT * FROM t_piperow;pg12@testdb$# END;pg12@testdb$# $$pg12@testdb-# LANGUAGE PLPGSQL;CREATE FUNCTIONTime: 3.850 ms[local]:5432 pg12@testdb=# select func_piperow_demo2() limit 5; func_piperow_demo2 -------------------------------------------------- (1,"2019-10-31 11:09:27.222996",test1,123455.55) (2,"2019-10-31 11:09:27.222996",test2,123455.55) (3,"2019-10-31 11:09:27.222996",test3,123455.55) (4,"2019-10-31 11:09:27.222996",test4,123455.55) (5,"2019-10-31 11:09:27.222996",test5,123455.55)(5 rows)Time: 5.645 ms[local]:5432 pg12@testdb=#第三种方式,使用PL/pgSQL,RETURN NEXT
[local]:5432 pg12@testdb=# select func_piperow_demo3() limit 5; func_piperow_demo3 -------------------------------------------------- (1,"2019-10-31 11:09:27.222996",test1,123455.55) (2,"2019-10-31 11:09:27.222996",test2,123455.55) (3,"2019-10-31 11:09:27.222996",test3,123455.55) (4,"2019-10-31 11:09:27.222996",test4,123455.55) (5,"2019-10-31 11:09:27.222996",test5,123455.55)(5 rows)Time: 5.588 ms[local]:5432 pg12@testdb=#参考资料
PostgreSQL Oracle 兼容性 之 TABLE、PIPELINED函数
Pipelined in Oracle as well in PostgreSQL
数据
函数
方式
有效
兼容性
参考资料
可以通过
性能
数据表
特性
用者
资料
多行
参考
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
可信计算将成网络安全
淮南旧服务器回收在线咨询
服务器双网卡作用
增城区新一代网络技术开发动态
和平精英高端服务器多少钱
张家口聊天软件开发诚信公司
电信承载网络技术支撑面试
ns暗黑2和pc共通服务器么
sql查询数据库表有哪些列
为什么要找软件开发
湖南服务器机柜厂家直销云主机
网络安全法知识竞赛答案
软件开发网络技术培训
软件开发产品助理
虚拟mfa软件开发
视频备份服务器搭建教程
上海网络安全准入控制系统哪家好
服务器直接接入网络安全
霞浦民宿软件开发
重庆科技学院网络技术就业怎么样
数据库好坏判断方法
虹口区软件开发文档
省网络安全工作座谈会
通用软件开发好吗
微信网络安全的黑板报
腾讯云服务器怎么删除登陆设备
sql 创建数据库权限
金华电子网络技术哪家好
网络安全工程师一年的工资多少
县委网信办网络安全总结