中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Oracle vs PostgreSQL Develop(19) - PIPE ROW

發布時間:2020-08-10 19:06:14 來源:ITPUB博客 閱讀:276 作者:husthxd 欄目:關系型數據庫

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 exist
TEST-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.9999
TEST-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 AM

PostgreSQL
下面來看看PG的實現,創建表,插入數據

[local]:5432 pg12@testdb=# drop table if exists t_piperow;
DROP TABLE
Time: 5.255 ms
[local]:5432 pg12@testdb=# create table t_piperow(id int,c1 timestamp ,c2 varchar(20),c3 float);
CREATE TABLE
Time: 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 500
Time: 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_piperow
pg12@testdb-# AS
pg12@testdb-# $$
pg12@testdb$#   SELECT * FROM t_piperow;
pg12@testdb$# $$
pg12@testdb-# LANGUAGE SQL;
CREATE FUNCTION
Time: 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_piperow
pg12@testdb-# AS
pg12@testdb-# $$
pg12@testdb$# BEGIN
pg12@testdb$#   RETURN QUERY SELECT * FROM t_piperow;
pg12@testdb$# END;
pg12@testdb$# $$
pg12@testdb-# LANGUAGE PLPGSQL;
CREATE FUNCTION
Time: 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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

高唐县| 龙陵县| 从化市| 铜山县| 屯昌县| 油尖旺区| 拉孜县| 中山市| 贡山| 台前县| 资源县| 苗栗县| 丽水市| 册亨县| 北宁市| 承德县| 新邵县| 琼结县| 苗栗市| 阜康市| 沙雅县| 富民县| 崇州市| 濮阳县| 扬州市| 长海县| 论坛| 渝中区| 泰兴市| 和静县| 屯昌县| 金门县| 桃园县| 淄博市| 城市| 尼玛县| 永定县| 酉阳| 张家口市| 庆云县| 电白县|