【发布时间】:2017-10-20 10:48:56
【问题描述】:
我需要从 Nodejs 中的 pg 函数中获取多个游标。
我在 PG 中尝试了两种函数编写方法,如下链接所述: http://www.sqlines.com/postgresql/how-to/return_result_set_from_stored_procedure
在 PG 管理中|||查询工具我可以通过给出它的名称来查看多个游标的输出。 我想从节点 js 应用程序中获取相同的内容,我正在使用“pg-promise”。
选项-1
db.func('get_data',
[
name,
key
])
.then(dbResult => {
console.log(" RES 1" + dbResult);
}).catch(error => {
console.log(error);
throw error;
});
选项 2
var retCursor1 = {};
var retCursor2 = {};
db.func('get_data',
[
name,
key,
retCursor1,
retCursor2,
])
.then(dbResult => {
console.log(" RES 1" + dbResult);
console.log(" RES 2" + retCursor1);
console.log(" RES 3" + retCursor2);
}).catch(error => {
console.log(error);
throw error;
});
get_data PG 函数将返回 2 个 refCursors。
但是没有运气,有人可以建议在 Node js 中获取多个光标的最佳方法是什么。
【问题讨论】:
-
您作为参数传递给
db.func的内容只能是输入参数。该语法不支持输出参数。 -
在 option-2 中输入前 2 个元素,但 refcursor1 和 2 被命名为游标,期望输出记录在 DB 中。我从 pg admin 尝试过的同类,我得到的结果和查询如下...... 'select get_data('SFO', 'FRA', 'result1', 'result2');获取所有“结果1”; FETCH all IN "result2";'
-
这不能直接与 pg-promise 一起工作,它需要使用游标的特殊方法。另见:stackoverflow.com/questions/12101773/…
-
最好是重新设计函数,以便您可以使用这种方法:github.com/vitaly-t/pg-promise/wiki/…
-
db.query( { text: "select get_data($1, $2, $3)", values: [ a, b, c ] }, function (err, r) { if (err) { db.query("COMMIT;"); } else { db.query('FETCH ALL FROM "
"', function (err, r1) { }); db.query('FETCH ALL FROM ""', function (err, r2) { }); db.query("COMMIT;"); }
标签: javascript node.js postgresql pg-promise