我有以下函数,它生成动态查询,最后我想将动态查询的结果插入到表中,但得到的错误是`

ERROR:  query has no destination for result data

HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function report_get_result(integer) line 46 at SQL statement
SQL statement "SELECT report_get_result(20150131)"
PL/pgSQL function inline_code_block line 2 at PERFORM
********** Error **********'

函数的主体是:
CREATE OR REPLACE FUNCTION report_get_result (
datekey integer
) returns setof logic_result_rcd
AS
$body$
DECLARE
    LogicID integer;
    SheetName text;
    Row_ID text;
    Column_ID text;
    FromTable text;
    Operation text;
    Amount text;
    CriteriaType_1 text;
    Function_1 text;
    Criteria_1 text;
    CriteriaType_2 text;
    Function_2 text;
    Criteria_2 text;
    CriteriaType_3 text;
    Function_3 text;
    Criteria_3 text;
    sql text;
    INC Integer;
begin
 DROP TABLE IF EXISTS loans;
 create temp table loans as
 select * from loan.vfact_state_principal where "DateKey" = datekey;

 DECLARE cursor_logic REFCURSOR;
 BEGIN
 OPEN cursor_logic for execute ('SELECT "LogicID" FROM logic_table_rcd');
 LOOP
    FETCH cursor_logic INTO INC;
    if not found then exit;
    end if;
    BEGIN
 SELECT  LogicID = "LogicID"
            ,SheetName = "SheetName"
            ,Row_ID = "Row_ID"::text
            ,Column_ID = "Column_ID"::text
            ,FromTable = "FromTable"
            ,Operation = "Operation"
            ,Amount = "Amount"
            ,CriteriaType_1 = CASE WHEN "CriteriaType_1" <> '' THEN ('WHERE
' || "CriteriaType_1") ELSE '' END
            ,Function_1 = CASE WHEN "Function_1" is null THEN 'Empty' ELSE
"Function_1" END
            ,Criteria_1 = CASE WHEN "Criteria_1" is null THEN 'Empty' ELSE
"Criteria_1" END
            ,CriteriaType_2 = CASE WHEN "CriteriaType_2" <> '' THEN ' AND '
|| "CriteriaType_2" ELSE '' END
            ,Function_2 = CASE WHEN "Function_2" is null THEN 'Empty' ELSE
"Function_2" END
            ,Criteria_2 = CASE WHEN "Criteria_2" is null THEN 'Empty' ELSE
"Criteria_2" END
            ,CriteriaType_3 = CASE WHEN "CriteriaType_3" <> '' THEN ' AND '
|| "CriteriaType_3" ELSE '' END
            ,Function_3 = CASE WHEN "Function_3" is null THEN 'Empty' ELSE
"Function_3" END
            ,Criteria_3 = CASE WHEN "Criteria_3" is null THEN 'Empty' ELSE
"Criteria_3" END
     FROM public.logic_table_rcd WHERE "LogicID" = INC;

sql:= 'INSERT INTO public.logic_result_rcd SELECT ' || INC::text || ', 1, '
|| EntityID::text || ', ' || DateKey::text || ', ' || 'RCD' || ', ' ||
SheetName::text || ', ' || Row_ID::text || ', ' || Column_ID::text || ', '
|| Operation || '(' || Amount || ')' || ' FROM ' || FromTable
    || CriteriaType_1 || ' ' || Function_1 || ' ' || Criteria_1
    || CriteriaType_2 || ' ' || Function_2 || ' ' || Criteria_2
    || CriteriaType_3 || ' ' || Function_3 || ' ' || Criteria_3;

RETURN QUERY EXECUTE sql;
END;
END LOOP;
CLOSE cursor_logic;
END;
END;

$body$

LANGUAGE plpgsql;

最佳答案

使用VAR_NAME := '';构造赋值,下面是一个示例:

t=# create function f(_i int) returns table (a int) as $$
declare sql text;
begin
 sql := format('select %s',_i);
 return query execute sql;
end;
$$
language plpgsql
;
CREATE FUNCTION
t=# select * From f(3);
 a
---
 3
(1 row)

10-08 04:43