2017-09-25 61 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