更新json列oracle v12.2

bjg7j2ky  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(423)

我遇到了一种需要在表中动态更新json列的情况。见下面的结构

create table tjson ( jsoncol CLOB CONSTRAINT tjson_chk CHECK (jsoncol IS JSON) );    

insert into tjson (jsoncol) VALUES ( '{"name" : "Kunal", "LName" : "Vohra" , 
                                       "salary" : "10000", "Age" : "25"}');

insert into tjson (jsoncol) VALUES ( '{"name" : "Rahul", "LName" : "Sharma" , 
                                       "salary" : "20000", "Age" : "35"}');

现在我需要改变 salary 为完整表提供一个基于 some_condition 我可以使用 JSON_VALUE(jsoncol, '$.Age') ```
update tjson
set jsoncol = '"salary":$JustChangeSalary'
where some_condition;

工资是动态的,不是固定的。我只想换薪水。
我试过了 `json_mergepatch` 但这只能从 `Oracle version 19` . 我们正在使用 `Oracle version 12.2` 
anhgbhbe

anhgbhbe1#

假设您想将kunal的薪水更新为15000,那么使用 JSON_EXISTS() 只在何处条件下起作用带他记录,而用传统的 REPLACE() set子句旁边的函数,其文字摘录包含与 salary 作为

UPDATE tjson 
   SET jsoncol = REPLACE( jsoncol, '"salary" : "10000"', '"salary" : "15000"' )
 WHERE JSON_EXISTS(jsoncol, '$.name?(@ == "Kunal")');

演示

pod7payv

pod7payv2#

之前 json_mergepatch 您可以使用基本的字符串函数,如 replace .
但是您需要注意这些-格式的差异可能会导致这些失败。您还可以更新符合条件的多个属性。
您可以通过以下方式在纯sql中安全地执行操作:
将json对象转换为具有 json_table 重建文档 json_object(agg) 以及 json_array(agg) ,根据需要传递新值。
例如:

create table tjson ( 
  jsoncol CLOB CONSTRAINT tjson_chk CHECK (jsoncol IS JSON) 
);

insert into tjson (jsoncol) VALUES ( 
  '{"name" : "Kunal", "LName" : "Vohra" , "salary" : "10000", "Age" : "25"}'
);

insert into tjson (jsoncol) VALUES ( 
  '{"name" : "Rahul", "LName" : "Sharma" , "salary" : "20000", "Age" : "35"}'
);

commit;

select json_object (
  'name' value j.name,
  'LName' value j.LName,
  'salary' value 30000, -- put new salary here
  'Age' value j.Age
) 
from   tjson, json_table (
  jsoncol, '$'
  columns (
    name path '$.name',
    LName path '$.LName',
    Age int path '$.Age'
  )
) j
where  j.name = 'Kunal';

JSON_OBJECT('NAME'VALUEJ.NAME,'LNAME'VALUEJ.LNAME,'SALARY'VALUE30000,--PUTNEWSALARYHERE'AGE'VALUEJ.AGE)   
{"name":"Kunal","LName":"Vohra","salary":30000,"Age":25}  

select t.jsoncol.name, t.jsoncol.salary
from   tjson t;

NAME    SALARY   
Kunal    10000     
Rahul    20000     

update tjson t
set    jsoncol = (
  select json_object (
    'name' value j.name,
    'LName' value j.LName,
    'salary' value 30000, -- put new salary here
    'Age' value j.Age
  ) 
  from   tjson, json_table (
    jsoncol, '$'
    columns (
      name path '$.name',
      LName path '$.LName',
      Age int path '$.Age'
    )
  ) j
  where t.jsoncol.name = j.name
)
where t.jsoncol.name = 'Kunal';

select t.jsoncol.name, t.jsoncol.salary
from   tjson t;

NAME    SALARY   
Kunal    30000     
Rahul    20000

显然这是。。。麻烦!对于复杂的文件来说是不切实际的。
幸运的是,从12.2开始,您可以使用pl/sql对象类型操作json文档:

declare
  jdoc tjson.jsoncol%type;
  jobj json_object_t;
begin
  select t.jsoncol
  into   jdoc
  from   tjson t
  where  t.jsoncol.name = 'Kunal';

  jobj := json_object_t.parse ( jdoc );
  jobj.put ( 'salary', 40000 );
  jdoc := jobj.to_clob();

  update tjson t
  set    jsoncol = jdoc
  where  t.jsoncol.name = 'Kunal';
end;
/

select t.jsoncol.name, t.jsoncol.salary
from   tjson t;

NAME    SALARY   
Kunal    40000     
Rahul    20000

相关问题