【发布时间】:2021-02-09 21:17:21
【问题描述】:
您好,我正在开发一个使用 postgres 的 Web 应用程序,其中一些 sql 位于存储过程/函数中。如果一个函数返回一个游标,我们如何在 nodejs 中检索和处理结果集?我正在使用 pg (node-postgres)。以下是我想从 nodejs 代码中调用的测试过程的代码。
drop table if exists tmp;
create table tmp
(
x integer
);
drop function if exists t1;
create or replace function t1( p_value integer)
returns refcursor
language plpgsql
as $$
declare
ref refcursor;
begin
--insert into tmp(x) values(p_value);
open ref for select x from tmp;
return ref;
end
$$;
【问题讨论】:
-
把它改成一个集合返回函数,你的生活会轻松很多。你甚至不需要 PL/pgSQL。
-
嗨,我的目标是返回结果集,以便我可以在中间层逐行处理它。这可以从其他语言中实现。我想知道 nodejs pg 是否允许这样做。
标签: node.js postgresql pg node-postgres