обновить json значений поля с привязкой переменных postgressql - PullRequest
0 голосов
/ 07 августа 2020

Я пытаюсь обновить значение поля json значением переменной привязки, я получаю ошибку ниже. Пожалуйста, помогите мне решить эту проблему.

    select * from tvmm_case where vmm_case_id = 3;
 vmm_case_id | case_sta_cd | submtr_id | business_id |       crt_ts        | regn_cd | iso_ctry_cd | sub_regn_cd |



                                                                                             

case_data

                                                                                      |    prgm_nm     | pct_compln | case_actv_ind |    last_mdfd_ts
      3 | 1           |        28 |          27 | 2010-10-22 06:24:00 |      38 |          38 |     
        24 | {"id": "001", "ppu": 0.55, "type": "donut", "nãme": "Cáke", "batter": "testing", "batter
    s": {"batter": [{"id": "1001", "size": 146, "type": "Regular", "ingredients": {"qty": "4$6", "product": "milk"}}, {"id": "1002", "size": "1,6", "type": "Chocolate", "ingredients": {"qty": "10*7", "produc
    t": "cocoa powder"}}, {"id": "1003", "size": 1, "type": "Blueberry", "ingredients": {"qty": "&?", "product": "fruits"}}, {"id": "1004", "size": 5, "type": "Devils Food", "ingredients": {"qty": 7, "produc
    t": "cream"}}]}, "topping": [{"id": "5001", "type": "None"}, {"id": "5002", "type": "Glazed"}, {"id": "5005", "type": "Sugar"}, {"id": "5007", "type": "Powdered Sugar"}, {"id": "5006", "type": "Chocolate
     with Sprinkles"}, {"id": "5003", "type": "Chocolate"}, {"id": "5004", "type": "Maple"}]} | Vmm Program-38 |         37 | Y             | 2010-10-22 06:24:00

 
\set id floor(random() * (100000 + 1));

UPDATE tvmm_case SET case_data = jsonb_set(case_data, "{id}" :id)  where case_data ->> 'id'  = '001' and vmm_case_id = 3  ;

ERROR:  syntax error at or near "floor"
LINE 1: ..._case SET case_data = jsonb_set(case_data, "{id}" floor(rand...

1 Ответ

0 голосов
/ 07 августа 2020

У вас отсутствует запятая, вы используете неправильные кавычки и вам нужно добавить явное приведение, поскольку целые числа неявно приводятся к jsonb.

... jsonb_set(case_data, '{id}', (:id)::text::jsonb) ...
...