簡體   English   中英

Postgresql用變量更新json數據

[英]Postgresql update json data with variables

我試圖在上一步中使用游標變量執行更新,但在 jsonb 字段中,系統返回錯誤。 競爭字段是 jsonb 類型。 我這樣做如下:

你能幫我嗎?

更新選項 1

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

錯誤選項 1

SQL 錯誤 [22P02]:錯誤:json 類型的輸入語法無效詳細信息:輸入字符串意外結束。 其中:JSON 數據,第 1 行:PL/pgSQL 函數 inline_code_block 第 72 行 SQL 語句

更新選項 2

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

錯誤選項 2

SQL 錯誤 [22P02]:錯誤:json 類型的無效輸入語法詳細信息:令牌“””無效。其中:JSON 數據,第 1 行:“PL/pgSQL 函數 inline_code_block 第 72 行在 SQL 語句中

首先,將整數列轉換為text然后轉換為jsonb

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

演示

非常感謝您的幫助。 對於字符串工作完美,但數值顯示錯誤消息。

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

錯誤:SQL 錯誤 [42846]:錯誤:無法將數字類型轉換為 jsonb 其中:PL/pgSQL 函數 inline_code_block 第 65 行在 SQL 語句中

有沒有其他方法可以轉換它?

示例數據:

'{"supplier": 1,"name": "Josh", "supplierId":10}'

非常感謝,

編輯:我解決了: content = jsonb_set(content, '{supplier}', supplyId::integer::text::jsonb)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM