亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

湾仔区| 三亚市| 阿勒泰市| 合阳县| 长乐市| 青岛市| 井研县| 阳春市| 宁蒗| 景宁| 故城县| 霍州市| 皮山县| 织金县| 宜兴市| 晋中市| 安顺市| 南涧| 芜湖县| 宁波市| 辛集市| 望城县| 天峻县| 金华市| 娄底市| 错那县| 英吉沙县| 抚松县| 黄浦区| 平南县| 衡阳市| 福建省| 宁晋县| 图片| 肃北| 渑池县| 遂宁市| 襄城县| 新泰市| 剑河县| 海林市|