有奖捉虫:办公协同&微信生态&物联网文档专题 HOT

返回值介绍

返回值可以是一个简单数据类型、复合类型、RECORD、已经存在的表行类型、表字段类型、游标、另外还可以返回一个记录集、如果不需要返回值,则可以用 RETURN void。返回值的字段名及类型可以在参数 OUT、INOUT 模式中声明。

返回值类型介绍

没有返回值

postgres=# CREATE OR REPLACE FUNCTION f8() RETURNS void AS
postgres-# $$
postgres$# BEGIN
postgres$# RAISE NOTICE '不用返回值,函数体可以有或没有return语句';
postgres$# RETURN ;--这一句可以有,也可以没有
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f8();
NOTICE: 不用返回值,函数体可以有或没有return语句
f8
----

(1 行记录)

返回简单类型

postgres=# CREATE OR REPLACE FUNCTION f9() RETURNS TEXT AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN 'tdsql_pg';
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f9() t(a_xm);
a_xm
------
tdsql_pg
(1 行记录)

postgres=#
postgres=# CREATE OR REPLACE FUNCTION f9(OUT a_xm TEXT) RETURNS TEXT AS
postgres-# $$
postgres$# BEGIN
postgres$# a_xm:='tdsql_pg';
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f9();
a_xm
------
tdsql_pg
(1 行记录)
上面两个函数其实就是同一个函数,建立时如果不加 OR REPLACE 则会提示已经存在。
postgres=# CREATE OR REPLACE FUNCTION f10() RETURNS TEXT[] AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN ARRAY['tdsql_pg','pgxz'];
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f10();
f10
---------------
{tdsql_pg,pgxz}
(1 行记录)

返回一个复合类型

postgres=# CREATE TYPE t_rec AS
postgres-# (
postgres(# id integer,
postgres(# mc text
postgres(# );
CREATE TYPE
postgres=#
postgres=# CREATE OR REPLACE FUNCTION f11() RETURNS t_rec AS
postgres-# $$
postgres$# DECLARE
postgres$# v_rec public.t_rec;
postgres$# BEGIN
postgres$# v_rec.id:=1;
postgres$# v_rec.mc='tdsql_pg';
postgres$# RETURN v_rec;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f11();
id | mc
----+------
1 | tdsql_pg
(1 行记录)

postgres=# CREATE OR REPLACE FUNCTION f12() RETURNS t_rec[] AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN ARRAY[ROW(1,'tdsql_pg'),ROW(1,'pgxz')]::t_rec[];
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f12();
f12
---------------------------
{"(1,tdsql_pg)","(1,pgxz)"}
(1 行记录)

返回行类型

postgres=# \\d t
资料表 "public.t"
栏位 | 型别 | 修饰词
------+---------+--------
id | integer |
mc | text |

postgres=# CREATE OR REPLACE FUNCTION f13() RETURNS public.t AS
postgres-# $$
postgres$# DECLARE
postgres$# v_rec public.t%ROWTYPE;
postgres$# BEGIN
postgres$# SELECT * INTO v_rec FROM public.t LIMIT 1;
postgres$# RETURN v_rec;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f13();
id | mc
----+------
1 | tdsql_pg
(1 行记录)

postgres=# CREATE OR REPLACE FUNCTION f14() RETURNS public.t[] AS
postgres-# $$
postgres$# DECLARE
postgres$# v_rec public.t[];
postgres$# BEGIN
postgres$# SELECT ARRAY[ROW(t.*),ROW(t.*)]::public.t[] INTO v_rec FROM public.t LIMIT 1;
postgres$# RETURN v_rec;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f14();
f14
-------------------------
{"(1,tdsql_pg)","(1,tdsql_pg)"}
(1 行记录)

返回 TABLE 类型

postgres=# DROP FUNCTION f14();
DROP FUNCTION
postgres=# CREATE FUNCTION f14() RETURNS TABLE(a_id integer, a_nc text) AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN QUERY SELECT 1::integer,'tdsql_pg'::Text;
postgres$# END;
postgres$# $$LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT * FROM f14();
a_id | a_nc
------+-------
1 | tdsql_pg
(1 row)

返回 RECORD 类型

postgres=# CREATE OR REPLACE FUNCTION f15() RETURNS RECORD AS
postgres-# $$
postgres$# DECLARE
postgres$# v_rec RECORD;
postgres$# BEGIN
postgres$# v_rec:=ROW(1::integer,'tdsql_pg'::text,'pgxz'::text);
postgres$# RETURN v_rec;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f15();
f15
-----------------
(1,tdsql_pg,pgxz)
(1 行记录)

postgres=# SELECT * FROM f15() t(id integer,xm text,xl text);
id | xm | xl
----+------+--------
1 | tdsql_pg | pgxz
(1 行记录)

返回一个游标

postgres=# CREATE OR REPLACE FUNCTION f16() RETURNS refcursor AS
postgres-# $$
postgres$# DECLARE
postgres$# v_ref refcursor;
postgres$# BEGIN
postgres$# OPEN v_ref FOR SELECT * FROM public.t;
postgres$# RETURN v_ref;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# BEGIN;
BEGIN
postgres=#
postgres=# SELECT * FROM f16();
f15
--------------------
<unnamed portal 1>
(1 行记录)

postgres=# FETCH ALL FROM "<unnamed portal 1>";
id | mc
----+--------
1 | tdsql_pg
2 | pgxz
(2 行记录)
postgres=# END;

postgres=# CREATE OR REPLACE FUNCTION f16(a_ref refcursor) RETURNS refcursor AS
postgres-# $$
postgres$# BEGIN
postgres$# OPEN a_ref FOR SELECT * FROM public.t;
postgres$# RETURN a_ref;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# BEGIN;
BEGIN
postgres=# SELECT * FROM f16('a');
f15
-----
a
(1 行记录)

postgres=# FETCH ALL FROM a;
id | mc
----+--------
1 | tdsql_pg
2 | pgxz
(2 行记录)

postgres=# END;
COMMIT

返回记录集

postgres=# CREATE OR REPLACE FUNCTION f17() RETURNS SETOF TEXT AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN NEXT 'tdsql_pg'::text;
postgres$# RETURN NEXT 'pgxz'::text;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f17();
f17
--------
tdsql_pg
pgxz
(2 行记录)

postgres=#
postgres=#
postgres=# CREATE OR REPLACE FUNCTION f18() RETURNS SETOF public.t AS
postgres-# $$
postgres$# DECLARE
postgres$# --使用行类型返回
postgres$# v_rec public.t%ROWTYPE;
postgres$# BEGIN
postgres$# FOR v_rec IN SELECT * FROM t ORDER BY id LOOP
postgres$# RETURN NEXT v_rec;
postgres$# END LOOP;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f18();
id | mc
----+--------
1 | tdsql_pg
2 | pgxz
(2 行记录)

postgres=# \\d t1
资料表 "public.t1"
栏位 | 型别 | 修饰词
------+---------------+--------
id | integer | 非空
yhm | text | 非空
nc | text | 非空
mm | character(32) | 非空
索引:
"t1_pkey" PRIMARY KEY, btree (id)
"t1_yhm_key" UNIQUE CONSTRAINT, btree (yhm)

postgres=# CREATE OR REPLACE FUNCTION f19() RETURNS SETOF public.t_rec AS
postgres-# $$
postgres$# DECLARE
postgres$# --使用已经定义的结构类型返回
postgres$# v_rec public.t_rec;
postgres$# BEGIN
postgres$# FOR v_rec IN SELECT id,yhm FROM t1 ORDER BY id LOOP
postgres$# RETURN NEXT v_rec;
postgres$# END LOOP;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f19();
id | mc
----+--------
1 | tdsql_pg
2 | pgxc
3 | pgxz
(3 行记录)

postgres=# CREATE OR REPLACE FUNCTION f20(a_int integer) RETURNS SETOF record AS
postgres-# $$
postgres$# DECLARE
postgres$# --a_int定义返回的字段数,实现动态列返回
postgres$# v_rec record;
postgres$# v_sql text;
postgres$# BEGIN
postgres$# IF a_int = 2 THEN
postgres$# v_sql:='SELECT id,yhm FROM t1 ORDER BY id ';
postgres$# ELSE
postgres$# v_sql:='SELECT id,yhm,nc FROM t1 ORDER BY id';
postgres$# END IF;
postgres$# FOR v_rec IN EXECUTE v_sql LOOP
postgres$# RETURN NEXT v_rec;
postgres$# END LOOP;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=# SELECT * FROM f20(2) t(id integer,yhm text);
id | yhm
----+--------
1 | tdsql_pg
2 | pgxc
3 | pgxz
(3 行记录)

postgres=# SELECT * FROM f20(3) t(id integer,yhm text,nc text);
id | yhm | nc
----+--------+--------
1 | tdsql_pg | tdsql_pg
2 | pgxc | pgxc
3 | pgxz | pgxz
(3 行记录)

postgres=# CREATE OR REPLACE FUNCTION f21(OUT a_id integer,OUT a_yhm TEXT) RETURNS SETOF record AS
postgres-# $$
postgres$# DECLARE
postgres$# --使用out返回
postgres$# v_rec record;
postgres$# BEGIN
postgres$# FOR v_rec IN SELECT id,yhm FROM t1 LOOP
postgres$# a_id:=v_rec.id;
postgres$# a_yhm:=v_rec.yhm;
postgres$# RETURN NEXT;
postgres$# END LOOP;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f21();
a_id | a_yhm
------+--------
1 | tdsql_pg
2 | pgxc
3 | pgxz
(3 行记录)

postgres=# CREATE OR REPLACE FUNCTION f22() RETURNS SETOF refcursor AS
postgres-# $$
postgres$# DECLARE
postgres$# --返回游标集
postgres$# v_ref1 REFCURSOR;
postgres$# v_ref2 REFCURSOR;
postgres$# BEGIN
postgres$# OPEN v_ref1 FOR SELECT * FROM t;
postgres$# OPEN v_ref2 FOR SELECT * FROM t1;
postgres$# RETURN NEXT v_ref1;
postgres$# RETURN NEXT v_ref2;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# BEGIN;
BEGIN
postgres=# SELECT * FROM f22();
f22
---------------------
<unnamed portal 13>
<unnamed portal 14>
(2 行记录)

postgres=# FETCH ALL FROM "<unnamed portal 13>";
id | mc
----+--------
1 | tdsql_pg
2 | pgxz
(2 行记录)

postgres=# FETCH ALL FROM "<unnamed portal 14>";
id | yhm | nc | mm
----+--------+--------+----------------------------------
1 | tdsql_pg | tdsql_pg | 202cb962ac59075b964b07152d234b70
2 | pgxc | pgxc | 202cb962ac59075b964b07152d234b70
3 | pgxz | pgxz | 202cb962ac59075b964b07152d234b70
(3 行记录)

postgres=# COMMIT;
COMMIT

postgres=# CREATE OR REPLACE FUNCTION f22(a_ref1 refcursor,a_ref2 refcursor) RETURNS SETOF refcursor AS
postgres-# $$
postgres$# BEGIN
postgres$# --指定游标名称
postgres$# OPEN a_ref1 FOR SELECT * FROM t;
postgres$# OPEN a_ref2 FOR SELECT * FROM t1;
postgres$# RETURN NEXT a_ref1;
postgres$# RETURN NEXT a_ref2;
postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# BEGIN;
BEGIN
postgres=#
postgres=# SELECT * FROM f22('a','b');
f22
-----
a
b
(2 行记录)


postgres=# FETCH ALL FROM "a";
id | mc
----+--------
1 | tdsql_pg
2 | pgxz
(2 行记录)


postgres=# FETCH ALL FROM "b";
id | yhm | nc | mm
----+--------+--------+----------------------------------
1 | tdsql_pg | tdsql_pg | 202cb962ac59075b964b07152d234b70
2 | pgxc | pgxc | 202cb962ac59075b964b07152d234b70
3 | pgxz | pgxz | 202cb962ac59075b964b07152d234b70
(3 行记录)


postgres=# COMMIT;
COMMIT

返回多态类型

postgres=# CREATE OR REPLACE FUNCTION f23(a_arg anyelement) RETURNS anyelement AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN a_arg;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f23('tdsql_pg'::text);
f23
------
tdsql_pg
(1 行记录)

postgres=# SELECT * FROM f23(1::integer);
f23
-----
1
(1 行记录)

postgres=# SELECT * FROM f23(ARRAY['tdsql_pg','pgxz']);
f23
---------------
{tdsql_pg,pgxz}
(1 行记录)

postgres=# SELECT * FROM f23(ROW(1,'tdsql_pg')::public.t_rec);
id | mc
----+------
1 | tdsql_pg
(1 行记录)

postgres=# CREATE OR REPLACE FUNCTION f24(a_arg ANYARRAY) RETURNS anyarray AS
postgres-# $$
postgres$# BEGIN
postgres$# RETURN a_arg;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f24(ARRAY[1,2]::INTEGER[]);
f24
-------
{1,2}
(1 行记录)

postgres=# SELECT f24(ARRAY[t1.*]) FROM t1;
f24
--------------------------------------------------------
{"(1,tdsql_pg,tdsql_pg,202cb962ac59075b964b07152d234b70)"}
{"(2,pgxc,pgxc,202cb962ac59075b964b07152d234b70)"}
{"(3,pgxz,pgxz,202cb962ac59075b964b07152d234b70)"}
(3 行记录)
返回数据类型如果是多态,则函数最少需要定义一个多态参数。