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

标签 postgresql

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

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 语句是否存在?

最佳答案

默认情况下 INSERT 不返回任何行,因此没有可获取的内容。您可以通过在 sql 字符串后附加 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

关于postgresql 无法打开 INSERT 查询作为游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423638/

相关文章:

python - 为什么 Django 创建带有时区的 Postgres 时间戳列?

c# - ORM 的哪些选择允许接近 'flick a switch' 在...SQL Server/SQL Azure 和 PostgreSQL/'Cloud' PostgreSQL 之间更改 RDBMS

postgresql - 为什么 PostgreSQL 的\dt 只显示公共(public)模式表?

python - 在 Post 请求正文中发送列表值以验证 Pydantic 模型

database - PostgreSQL 性能问题

php - 设置字段时的 Postgres 查询 SELECT

php - 无法使用 Doctrine 创建 postgres 数据库

entity-framework - 将 EDMX 设计器与 PostgreSQL 结合使用

mysql - Rails Multi-Tenancy 架构,限定对多个租户的访问

恢复后Postgresql自动递增ID?