Kas*_*sen 2 sql postgresql plpgsql node.js
我正在运行 PostgreSQL 和 NodeJS。
在 PostgreSQL 中,我有一个自定义函数dummy:
... RETURNS RECORD AS $$
...
DECLARE ret RECORD
...
RETURN ret;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
在 NodeJS 中,这将返回一个tuple, 作为 a string:
(x,y,z)
Run Code Online (Sandbox Code Playgroud)
所以我必须手动拆分字符串并读取部分......是否有可能让 PostgreSQL 返回tuplearow或类似的,所以我可以在 NodeJS 中使用data.x,data.y和data.z?
fn():
t=# create or replace function f() returns record as $$ declare ret record; begin for ret in (select oid::int,datname::text from pg_database limit 1) loop return ret; end loop; end; $$ language plpgsql;
CREATE FUNCTION
Run Code Online (Sandbox Code Playgroud)
您的来电:
t=# select f();
f
------------------
(13505,postgres)
(1 row)
Run Code Online (Sandbox Code Playgroud)
预期电话:
t=# select * from f() as r(o int,n text);
o | n
-------+----------
13505 | postgres
(1 row)
Run Code Online (Sandbox Code Playgroud)
如果你想预先定义隐式调用的记录结构,你可以创建一个虚拟的 fn():
t=# create or replace function dummy() returns table (a int,b text) as $$ begin return query select * from f() as r(o int,n text); end; $$ language plpgsql;
CREATE FUNCTION
t=# select * from dummy();
a | b
-------+----------
13505 | postgres
(1 row)
Run Code Online (Sandbox Code Playgroud)
如果你想避免FROM:
t=# select (dummy()).*;
a | b
-------+----------
13505 | postgres
(1 row)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |