gpt4 book ai didi

json - Postgresql 使用变量更新 json 数据

转载 作者:行者123 更新时间:2023-11-29 13:09:00 24 4
gpt4 key购买 nike

我试图在上一步中使用游标变量执行更新,但在 jsonb 字段中,系统返回错误。 Contend 字段是一个 jsonb 类型。我是这样做的:

¿你能帮帮我吗?

更新选项 1

update test
set
content = jsonb_set(content, '{supplier}', '' || supplierId || ''::jsonb)
where operation_type = 'DEL';

错误选项 1

SQL Error [22P02]: ERROR: invalid input syntax for type json Detail: The input string ended unexpectedly. Where: JSON data, line 1: PL/pgSQL function inline_code_block line 72 at SQL statement

更新选项 2

update test
set
content = jsonb_set(content, '{supplier}', '"' || supplierId || '"'::jsonb)
where operation_type = 'DEL';

错误选项 2

SQL Error [22P02]: ERROR: invalid input syntax for type json Detail: Token """ is invalid. Where: JSON data, line 1: " PL/pgSQL function inline_code_block line 72 at SQL statement

最佳答案

首先,将整数列转换为text,然后转换为jsonb

jsonb_set(content, '{supplier}',  
supplierId :: text :: jsonb)

DEMO

关于json - Postgresql 使用变量更新 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com