PostgreSQL函式裡呼叫函式(SETOF + RETURN QUERY)

shytodear發表於2018-12-07

PostgreSQL函式裡呼叫函式(SETOF + RETURN QUERY)


1、函式如下:
CREATE OR REPLACE FUNCTION public.get_version() RETURNS SETOF text AS
$$
BEGIN
     RETURN query select version();
END
$$
language plpgsql;


2、實驗記錄:
postgres=# CREATE OR REPLACE FUNCTION public.get_version() RETURNS SETOF text AS
postgres-# $$
postgres$# BEGIN
postgres$#     RETURN query select * from version();
postgres$# END
postgres$# $$
postgres-# language plpgsql;
CREATE FUNCTION
postgres=#
postgres=#
postgres=# select get_version();
                                               get_version                                              
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
(1 row)

postgres=# select version();
                                                 version                                                
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
(1 row)

postgres=# select * from version();
                                                 version                                                
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
(1 row)

postgres=#
postgres=#
postgres=# CREATE OR REPLACE FUNCTION public.get_version1() RETURNS SETOF text AS
postgres-# $$
postgres$# BEGIN
postgres$#     RETURN query select version();
postgres$# END
postgres$# $$
postgres-# language plpgsql;
CREATE FUNCTION
postgres=# select get_version1();
                                              get_version1                                              
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
(1 row)

postgres=# select * from get_version1();
                                              get_version1                                              
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
(1 row)

postgres=#

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27126919/viewspace-2284475/,如需轉載,請註明出處,否則將追究法律責任。

相關文章