获取存储过程引用输出并插入临时表

问题描述

我有一个存储过程 (P1),它返回一个 refcursor 和一些其他文本数据类型值。 我有一个过程(P2),我需要获取 P1 的 refcursor 输出并将其插入到临时表中。临时表具有匹配的列和数据类型。

create or replace procedure P1(inout rfcur refcursor,in dtl text)
as
$$
begin
open rfcur for select * from tst_dump where ident = dtl;
end;
$$
language plpgsql;

create or replace P2(inout rfc refcursor,in dt_array text[])
as
$$
declare
i record;
cur refcursor;
begin
for i in array_lower(dt_array,1)..array_upper(dt_array,1) loop
call P1(cur,i);
--I need to fetch the result set from `cur` and store into a temp table `t_tab1`.
end loop;
end;
$$
language plpgsql;

是否可以在 Postgres 中实现这一点?

注意:我不应该对程序 P1 进行任何更改。

解决方法

p2 可能如下所示:

CREATE PROCEDURE p2(IN dt_array text[])
   LANGUAGE plpgsql AS
$$DECLARE
   r record;
   i integer;
   cur refcursor;
BEGIN
   FOR i IN array_lower(dt_array,1)..array_upper(dt_array,1) LOOP
      CALL p1(cur,i::text);

      LOOP
         FETCH cur INTO r;
         EXIT WHEN NOT FOUND;
         INSERT INTO t_tab1 (...) VALUES (r.col1,r.col2,...;
      END LOOP;
   END LOOP;
END;$$;

你应该缩进你的代码。这是您编程时的基本要求。

在我看来,你做的事情不对。使用过程和游标会使一切变得复杂并使其变慢。

你应该做类似的事情

INSERT INTO t_tab
SELECT /* your original query */;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...