postgresql无法打开INSERT查询作为游标

时间:2017-09-26 10:12:48

标签: postgresql

我正在尝试生成动态查询以将动态select的结果插入到表中。我的代码如下。

CREATE OR REPLACE FUNCTION public.report_get_result(
datekey integer)
RETURNS setof public.logic_result_rcd
LANGUAGE 'plpgsql'
COST 100
VOLATILE 
AS $BODY$

DECLARE
     LogicID text;
     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 SELECT "LogicID" FROM logic_table_rcd;
 LOOP
    FETCH cursor_logic INTO INC;
    if not found then exit;
    end if;
    BEGIN

    select into LogicID "LogicID" from public.logic_table_rcd WHERE 
"LogicID" = 1;
     select into SheetName "SheetName" from public.logic_table_rcd WHERE 
"LogicID" = 1;
     select into Row_ID "Row_ID" from public.logic_table_rcd WHERE "LogicID" 
= 1;
     select into Column_ID "Column_ID" from public.logic_table_rcd WHERE 
"LogicID" = 1;
     select into FromTable "FromTable" from public.logic_table_rcd WHERE 
"LogicID" = 1;
     select into Operation "Operation" from public.logic_table_rcd WHERE 
"LogicID" = 1;
     select into Amount "Amount" from public.logic_table_rcd WHERE "LogicID" 
= 1;
     select into CriteriaType_1 CASE WHEN "CriteriaType_1" <> '' OR 
"CriteriaType_1" is not null THEN (' WHERE "' || "CriteriaType_1" || '"') 
ELSE '' END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into Function_1 CASE WHEN "Function_1" is null THEN '' ELSE 
"Function_1" END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into Criteria_1 CASE WHEN "Criteria_1" is null THEN '' ELSE 
"Criteria_1" END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into CriteriaType_2 CASE WHEN "CriteriaType_2" <> '' OR 
"CriteriaType_2" is not null THEN ' AND "' || "CriteriaType_2" || '"' ELSE 
'' END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into Function_2 CASE WHEN "Function_2" is null THEN '' ELSE 
"Function_2" END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into Criteria_2 CASE WHEN "Criteria_2" is null THEN '' ELSE 
"Criteria_2" END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into CriteriaType_3 CASE WHEN "CriteriaType_3" <> '' or 
"CriteriaType_3" is not null THEN ' AND "' || "CriteriaType_3" || '"' ELSE 
'' END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into Function_3 CASE WHEN "Function_3" is null THEN '' ELSE 
"Function_3" END from public.logic_table_rcd WHERE "LogicID" = 1;
     select into Criteria_3 CASE WHEN "Criteria_3" is null THEN '' ELSE 
"Criteria_3" END from public.logic_table_rcd WHERE "LogicID" = 1;

 sql:= 'INSERT INTO public.logic_result_rc SELECT ' || INC::text || ', 1, ' 
|| 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$;
ALTER FUNCTION public.report_get_result(integer)
OWNER TO postgres;

但执行后我得到了下一个错误:

 cannot open INSERT query as cursor

正确分配所有变量。可能是插入必须在光标外的其他地方? INSERT INTO .... FETCH ALL语句是否存在?

1 个答案:

答案 0 :(得分:1)

默认情况下,INSERT不返回任何行,因此无需获取任何内容。您可以通过在RETURNING *附加public.logic_result_rc字符串来解决此问题,并将内容插入RETURN QUERY EXECUTE concat(sql, ' RETURNING *');

所以它希望如此:INSERT INTO table_name ( column_name [, ...] ) VALUES ( ) | query RETURNING * --or list of columns, same syntax like for SELECT

基本语法是:

string[] files = System.IO.Directory.GetFiles(path, "*.txt");