返回值介绍
返回值可以是一个简单数据类型、复合类型、RECORD、已经存在的表行类型、表字段类型、游标、另外还可以返回一个记录集、如果不需要返回值,则可以用 RETURN void。返回值的字段名及类型可以在参数 OUT、INOUT 模式中声明。
返回值类型介绍
没有返回值
postgres=# CREATE OR REPLACE FUNCTION f8() RETURNS void ASpostgres-# $$postgres$# BEGINpostgres$# RAISE NOTICE '不用返回值,函数体可以有或没有return语句';postgres$# RETURN ;--这一句可以有,也可以没有postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f8();NOTICE: 不用返回值,函数体可以有或没有return语句f8----(1 行记录)
返回简单类型
postgres=# CREATE OR REPLACE FUNCTION f9() RETURNS TEXT ASpostgres-# $$postgres$# BEGINpostgres$# RETURN 'tdsql_pg';postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#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 ASpostgres-# $$postgres$# BEGINpostgres$# a_xm:='tdsql_pg';postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f9();a_xm------tdsql_pg(1 行记录)
上面两个函数其实就是同一个函数,建立时如果不加 OR REPLACE 则会提示已经存在。
postgres=# CREATE OR REPLACE FUNCTION f10() RETURNS TEXT[] ASpostgres-# $$postgres$# BEGINpostgres$# RETURN ARRAY['tdsql_pg','pgxz'];postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f10();f10---------------{tdsql_pg,pgxz}(1 行记录)
返回一个复合类型
postgres=# CREATE TYPE t_rec ASpostgres-# (postgres(# id integer,postgres(# mc textpostgres(# );CREATE TYPEpostgres=#postgres=# CREATE OR REPLACE FUNCTION f11() RETURNS t_rec ASpostgres-# $$postgres$# DECLAREpostgres$# v_rec public.t_rec;postgres$# BEGINpostgres$# v_rec.id:=1;postgres$# v_rec.mc='tdsql_pg';postgres$# RETURN v_rec;postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f11();id | mc----+------1 | tdsql_pg(1 行记录)postgres=# CREATE OR REPLACE FUNCTION f12() RETURNS t_rec[] ASpostgres-# $$postgres$# BEGINpostgres$# RETURN ARRAY[ROW(1,'tdsql_pg'),ROW(1,'pgxz')]::t_rec[];postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#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 ASpostgres-# $$postgres$# DECLAREpostgres$# v_rec public.t%ROWTYPE;postgres$# BEGINpostgres$# SELECT * INTO v_rec FROM public.t LIMIT 1;postgres$# RETURN v_rec;postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f13();id | mc----+------1 | tdsql_pg(1 行记录)postgres=# CREATE OR REPLACE FUNCTION f14() RETURNS public.t[] ASpostgres-# $$postgres$# DECLAREpostgres$# v_rec public.t[];postgres$# BEGINpostgres$# 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 FUNCTIONpostgres=#postgres=# SELECT * FROM f14();f14-------------------------{"(1,tdsql_pg)","(1,tdsql_pg)"}(1 行记录)
返回 TABLE 类型
postgres=# DROP FUNCTION f14();DROP FUNCTIONpostgres=# CREATE FUNCTION f14() RETURNS TABLE(a_id integer, a_nc text) ASpostgres-# $$postgres$# BEGINpostgres$# RETURN QUERY SELECT 1::integer,'tdsql_pg'::Text;postgres$# END;postgres$# $$LANGUAGE plpgsql;CREATE FUNCTIONpostgres=# SELECT * FROM f14();a_id | a_nc------+-------1 | tdsql_pg(1 row)
返回 RECORD 类型
postgres=# CREATE OR REPLACE FUNCTION f15() RETURNS RECORD ASpostgres-# $$postgres$# DECLAREpostgres$# v_rec RECORD;postgres$# BEGINpostgres$# v_rec:=ROW(1::integer,'tdsql_pg'::text,'pgxz'::text);postgres$# RETURN v_rec;postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=# 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 ASpostgres-# $$postgres$# DECLAREpostgres$# v_ref refcursor;postgres$# BEGINpostgres$# OPEN v_ref FOR SELECT * FROM public.t;postgres$# RETURN v_ref;postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# BEGIN;BEGINpostgres=#postgres=# SELECT * FROM f16();f15--------------------<unnamed portal 1>(1 行记录)postgres=# FETCH ALL FROM "<unnamed portal 1>";id | mc----+--------1 | tdsql_pg2 | pgxz(2 行记录)postgres=# END;postgres=# CREATE OR REPLACE FUNCTION f16(a_ref refcursor) RETURNS refcursor ASpostgres-# $$postgres$# BEGINpostgres$# OPEN a_ref FOR SELECT * FROM public.t;postgres$# RETURN a_ref;postgres$# END;postgres$# $$postgres-# LANGUAGE plpgsql;CREATE FUNCTIONpostgres=#postgres=# BEGIN;BEGINpostgres=# SELECT * FROM f16('a');f15-----a(1 行记录)postgres=# FETCH ALL FROM a;id | mc----+--------1 | tdsql_pg2 | pgxz(2 行记录)postgres=# END;COMMIT
返回记录集
postgres=# CREATE OR REPLACE FUNCTION f17() RETURNS SETOF TEXT ASpostgres-# $$postgres$# BEGINpostgres$# RETURN NEXT 'tdsql_pg'::text;postgres$# RETURN NEXT 'pgxz'::text;postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f17();f17--------tdsql_pgpgxz(2 行记录)postgres=#postgres=#postgres=# CREATE OR REPLACE FUNCTION f18() RETURNS SETOF public.t ASpostgres-# $$postgres$# DECLAREpostgres$# --使用行类型返回postgres$# v_rec public.t%ROWTYPE;postgres$# BEGINpostgres$# FOR v_rec IN SELECT * FROM t ORDER BY id LOOPpostgres$# RETURN NEXT v_rec;postgres$# END LOOP;postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f18();id | mc----+--------1 | tdsql_pg2 | 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 ASpostgres-# $$postgres$# DECLAREpostgres$# --使用已经定义的结构类型返回postgres$# v_rec public.t_rec;postgres$# BEGINpostgres$# FOR v_rec IN SELECT id,yhm FROM t1 ORDER BY id LOOPpostgres$# RETURN NEXT v_rec;postgres$# END LOOP;postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=#postgres=# SELECT * FROM f19();id | mc----+--------1 | tdsql_pg2 | pgxc3 | pgxz(3 行记录)postgres=# CREATE OR REPLACE FUNCTION f20(a_int integer) RETURNS SETOF record ASpostgres-# $$postgres$# DECLAREpostgres$# --a_int定义返回的字段数,实现动态列返回postgres$# v_rec record;postgres$# v_sql text;postgres$# BEGINpostgres$# IF a_int = 2 THENpostgres$# v_sql:='SELECT id,yhm FROM t1 ORDER BY id ';postgres$# ELSEpostgres$# v_sql:='SELECT id,yhm,nc FROM t1 ORDER BY id';postgres$# END IF;postgres$# FOR v_rec IN EXECUTE v_sql LOOPpostgres$# RETURN NEXT v_rec;postgres$# END LOOP;postgres$# RETURN ;--最后的RETURN可以加,也可以不加上去postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=# SELECT * FROM f20(2) t(id integer,yhm text);id | yhm----+--------1 | tdsql_pg2 | pgxc3 | pgxz(3 行记录)postgres=# SELECT * FROM f20(3) t(id integer,yhm text,nc text);id | yhm | nc----+--------+--------1 | tdsql_pg | tdsql_pg2 | pgxc | pgxc3 | pgxz | pgxz(3 行记录)postgres=# CREATE OR REPLACE FUNCTION f21(OUT a_id integer,OUT a_yhm TEXT) RETURNS SETOF record ASpostgres-# $$postgres$# DECLAREpostgres$# --使用out返回postgres$# v_rec record;postgres$# BEGINpostgres$# FOR v_rec IN SELECT id,yhm FROM t1 LOOPpostgres$# 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 FUNCTIONpostgres=#postgres=# SELECT * FROM f21();a_id | a_yhm------+--------1 | tdsql_pg2 | pgxc3 | pgxz(3 行记录)postgres=# CREATE OR REPLACE FUNCTION f22() RETURNS SETOF refcursor ASpostgres-# $$postgres$# DECLAREpostgres$# --返回游标集postgres$# v_ref1 REFCURSOR;postgres$# v_ref2 REFCURSOR;postgres$# BEGINpostgres$# 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 FUNCTIONpostgres=#postgres=# BEGIN;BEGINpostgres=# SELECT * FROM f22();f22---------------------<unnamed portal 13><unnamed portal 14>(2 行记录)postgres=# FETCH ALL FROM "<unnamed portal 13>";id | mc----+--------1 | tdsql_pg2 | pgxz(2 行记录)postgres=# FETCH ALL FROM "<unnamed portal 14>";id | yhm | nc | mm----+--------+--------+----------------------------------1 | tdsql_pg | tdsql_pg | 202cb962ac59075b964b07152d234b702 | pgxc | pgxc | 202cb962ac59075b964b07152d234b703 | pgxz | pgxz | 202cb962ac59075b964b07152d234b70(3 行记录)postgres=# COMMIT;COMMITpostgres=# CREATE OR REPLACE FUNCTION f22(a_ref1 refcursor,a_ref2 refcursor) RETURNS SETOF refcursor ASpostgres-# $$postgres$# BEGINpostgres$# --指定游标名称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 FUNCTIONpostgres=#postgres=# BEGIN;BEGINpostgres=#postgres=# SELECT * FROM f22('a','b');f22-----ab(2 行记录)postgres=# FETCH ALL FROM "a";id | mc----+--------1 | tdsql_pg2 | pgxz(2 行记录)postgres=# FETCH ALL FROM "b";id | yhm | nc | mm----+--------+--------+----------------------------------1 | tdsql_pg | tdsql_pg | 202cb962ac59075b964b07152d234b702 | pgxc | pgxc | 202cb962ac59075b964b07152d234b703 | pgxz | pgxz | 202cb962ac59075b964b07152d234b70(3 行记录)postgres=# COMMIT;COMMIT
返回多态类型
postgres=# CREATE OR REPLACE FUNCTION f23(a_arg anyelement) RETURNS anyelement ASpostgres-# $$postgres$# BEGINpostgres$# RETURN a_arg;postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=#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 ASpostgres-# $$postgres$# BEGINpostgres$# RETURN a_arg;postgres$# END;postgres$# $$postgres-# LANGUAGE PLPGSQL;CREATE FUNCTIONpostgres=#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 行记录)
返回数据类型如果是多态,则函数最少需要定义一个多态参数。