There are many Postgresql stored procedure returns a value, which is recorded in advance only through your own use, and slowly expand
1, type type, where the geometry can be any type of postgresql support (integer / text / character varying .....)
CREATE OR REPLACE FUNCTION test( tbl character varying) RETURNS geometry AS $BODY$ DECLARE v_res the Geometry; - the end result the begin return v_res; End ; $BODY$ LANGUAGE plpgsql VOLATILE STRICT COST 100; ALTER FUNCTION test(character varying) OWNER TO postgres;
2, table type, a return data set, define a return needs its own field, here select query execution returns with return
CREATE OR REPLACE FUNCTION test( IN tbl character varying) RETURNS TABLE(v_gid integer, v_res geometry) AS $BODY$ DECLARE the begin - execution returns a result return Query SELECT v_uptap_gid AS res_uptap_gid, v_uptap_geom AS res_uptap_geom; End ; $BODY$ LANGUAGE plpgsql VOLATILE STRICT COST 100 ROWS 1000; ALTER FUNCTION test(character varying) OWNER TO postgres;
3, setof table / view model, returned to a data set, the data returned is usually a table lookup
But it must be an existing field in the table, the new field will not do (for example, I want to identify the type 'select type, a. * From tb a' upon return)
Convenient place is not defined return force field ( 'select * from tb' may be)
CREATE OR REPLACE FUNCTION test( IN tbl character varying) RETURNS SETOF table1 AS $BODY$ declare begin SELECT * from table1; --或者 return query select * from table1; end; $BODY$ LANGUAGE plpgsql VOLATILE STRICT COST 100 ROWS 1000; ALTER FUNCTION test(character varying) OWNER TO postgres;
3, setof record type A data set is returned, similar to the setof table
The difference is that I can return setof record all the fields, calling for people to take the field
CREATE OR REPLACE FUNCTION test( IN tbl character varying) RETURNS SETOF record AS $BODY$ declare r record; begin for r in EXECUTE 'select * from tb' loop return next r; end loop; return; end; $BODY$ LANGUAGE plpgsql VOLATILE STRICT COST 100 ROWS 1000; ALTER FUNCTION test(character varying) OWNER TO postgres; --调用 select * from test('tb') as member(id int, name text);