PostgreSQL functions (stored procedure) returns an implementation of a plurality of records
Background: The use of function pg offer returns multiple records form rendering
Way: by cyclic
CREATE
OR REPLACE FUNCTION "public"."f_get_traindata" ( OUT "o_item" VARCHAR, OUT "o_periodsnum" VARCHAR, OUT "o_trainday" VARCHAR, OUT "o_attendpersonnum" VARCHAR ) RETURNS SETOF "pg_catalog"."record" AS $BODY$ DECLARE
rec record; -- 定义返回记录行
o_sql TEXT; -- 定义执行的sql片段
BEGIN
o_sql := 'SELECT item,periodsnum,trainday,attendpersonnum FROM bi_fact_traindata;';
-- 输出字段
FOR rec IN EXECUTE o_sql
loop
-- 将每个字段一一赋值给变量
o_item := rec.item;
o_periodsnum := rec.periodsnum;
o_trainday := rec.trainday;
o_attendpersonnum := rec.attendpersonnum;
RETURN NEXT;
END loop;
RETURN;
END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000
In this way too much trouble, you need a custom field, more complicated.
Second way: Using the table
Use table
CREATE OR REPLACE FUNCTION "public"."f_get_appraisalindex" ( "o_indexid" VARCHAR )
-- 定义table
RETURNS TABLE (
"o_indexname" VARCHAR,
"o_standardrule" VARCHAR,
"o_standardscore" VARCHAR,
"o_losescore" NUMERIC,
"o_orgstructname" VARCHAR,
"o_appraisaldate" VARCHAR,
"o_periodictime" VARCHAR,
"o_periodictimelosescore" NUMERIC,
"o_evaluatereason" VARCHAR
) AS $BODY$
BEGIN
RETURN query
-- 字段一一映射
SELECT
tn_indexname,
tn_standardrule,
tn_standardscore,
tn_losescore,
tn_orgstructname,
tn_appraisaldate,
tn_periodictime,
tn_periodictimelosescore,
tn_evaluatereason
FROM
tn_appraisalindex
WHERE
tn_periodictimelosescore > 0
ORDER BY
tn_indexid;
END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000
Spread
How to define the situation there to the Senate and the Senate: do not need to be clear that the return way
(Source: https: //blog.csdn.net/qq_42535651/article/details/92089510)