2017-09-25 62 views
0

我有以下功能,生成动态查询,并在结束时,我想插入动态查询到表的结果,但我得到的错误是'PostgreSQL的插入动态查询结果放入表中的光标

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; 
+0

select into ...或执行select(舍弃结果) –

+0

谢谢。但我是新来的postgres,不知道如何写入选择...或执行选择(丢弃结果)。你能写一部分不正确的代码吗?在选择逻辑ID =“逻辑ID”时,发生错误 – Chernusha

+0

SELECT Sheet1 =“SheetID =”假设你根本就想把sql的sql作为文本赋值给变量sql,但是我不能告诉它 - 因为你的文章没有评论 –

回答

0

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) 
+0

谢谢。我已经成功地为变量赋值。现在,问题是“错误:无法打开INSERT查询作为游标”。 – Chernusha

+0

张贴代码,错误和问题作为一个新的职位,请 –

+0

https://stackoverflow.com/questions/46423638/postgresql-cannot-open-insert-query-as-cursor – Chernusha