Sql 更新json列oracle v12.2
我需要在表中动态更新json列。见下文结构Sql 更新json列oracle v12.2,sql,json,oracle,sql-update,oracle12c,Sql,Json,Oracle,Sql Update,Oracle12c,我需要在表中动态更新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
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"}');
现在,我需要根据某些条件将薪资
更改为完整表的动态值
我可以使用JSON\u值(jsoncol,$.Age')
工资是动态的,不是固定的。我愿意只改变薪水
我尝试了
json\u mergepatch
,但这只能从Oracle版本19
中获得。我们使用的是Oracle 12.2版假设您希望将Kunal的工资更新为15000,然后在WHERE条件下使用JSON_EXISTS()
SET子句旁边的函数,其文字摘录包含与薪资相关的键值组合
UPDATE tjson
SET jsoncol = REPLACE( jsoncol, '"salary" : "10000"', '"salary" : "15000"' )
WHERE JSON_EXISTS(jsoncol, '$.name?(@ == "Kunal")');
在json\u mergepatch
之前,您可以使用基本的字符串函数,如replace
但是您需要注意这些-格式的差异可能会导致这些失败。您还可以更新符合条件的多个属性
您可以通过以下方式在纯SQL中安全地执行操作:
- 使用
JSON\u表将JSON对象转换为行和列
- 使用
json\u对象(agg)
和json\u数组(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
它失败了。SQL错误:ORA-40442:JSON路径表达式语法错误@BarbarosÖzhan该问题与一个bug有关。它的温度也在12摄氏度以内。咨询你的管理员,告诉他需要应用最新的JSON补丁包,其中包括bug.18999568的补丁(补丁编号参考请参见Note.1992767.1)@KunalVohra。谢谢@BarbarosÖzhan。同时,我又问了一个与oracle DB脚本相关的问题,您能看一下吗
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