您知道使用命名字段表示法创建 ROW 的 json_populate_record 替代方法吗?

发布于 2025-01-12 11:17:10 字数 915 浏览 2 评论 0原文

在这个例子中:

CREATE TYPE contact AS (
   firstname VARCHAR,
   lastname VARCHAR
);

postgres=# SELECT json_populate_record(NULL::contact,
postgres(#   '{
postgres'#      "firstname": "John",
postgres'#      "lastname": "Doe"
postgres'#    }'
postgres'# );
 json_populate_record
----------------------
 (John,Doe)
(1 row)

问题:你知道像 json_populate_record,它允许在不使用 json 格式的情况下创建具有命名字段表示法的 ROW

我知道 ROW 语法表达式:

postgres=# SELECT ROW('John', 'Doe')::contact;
    row
------------
 (John,Doe)
(1 row)

但我没有找到 ROW 构造函数 语法允许命名字段表示法。

谨致问候,
史蒂芬

In this example:

CREATE TYPE contact AS (
   firstname VARCHAR,
   lastname VARCHAR
);

postgres=# SELECT json_populate_record(NULL::contact,
postgres(#   '{
postgres'#      "firstname": "John",
postgres'#      "lastname": "Doe"
postgres'#    }'
postgres'# );
 json_populate_record
----------------------
 (John,Doe)
(1 row)

Question: do you know a method like json_populate_record, which allows creating a ROW with named field notation without using a json format?

I know the ROW syntax expression:

postgres=# SELECT ROW('John', 'Doe')::contact;
    row
------------
 (John,Doe)
(1 row)

But I didn't find a ROW constructors syntax allowing a named field notation.

Best regards,
Stéphane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2025-01-19 11:17:10

行构造函数的命名参数没有选项。您可以通过定义与早期定义的类型相关的函数来模拟类似的语法,例如

create type contact as (
    firstname text,
    lastname text
);

create or replace function make_contact(firstname text, lastname text)
returns contact language sql immutable as $
    select $1, $2
$;

-- use
select make_contact(firstname=>'John', lastname=>'Doe');

db< 中测试它;>小提琴。

There is no option for named arguments of a row constructor. You can simulate alike syntax by defining a function related to an earlier defined type, e.g.

create type contact as (
    firstname text,
    lastname text
);

create or replace function make_contact(firstname text, lastname text)
returns contact language sql immutable as $
    select $1, $2
$;

-- use
select make_contact(firstname=>'John', lastname=>'Doe');

Test it in db<>fiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文