在oracle中更新json值
表中有一列以json格式存储数据 例如,专栏:在oracle中更新json值,json,oracle,Json,Oracle,表中有一列以json格式存储数据 例如,专栏: select jsoncol from table; --它给 {"y" : "4", "n" : "0" , "default" : "4"} 当我尝试 select json_value(jsoncol, '$.n') from table; 我得到值0 如何将n的值更新为2 {"y" : "4", "n" : "2" , "default" : "4"} 您可以在PL/SQL中使用JSON\u ELEMENT\u T.parse和.
select jsoncol from table;
--它给
{"y" : "4", "n" : "0" , "default" : "4"}
当我尝试
select json_value(jsoncol, '$.n') from table;
我得到值0
如何将n的值更新为2
{"y" : "4", "n" : "2" , "default" : "4"}
您可以在PL/SQL中使用
JSON\u ELEMENT\u T.parse
和.PUT
方法
设置
create table tjson ( jsoncol CLOB CONSTRAINT tjson_chk CHECK (jsoncol IS JSON) );
insert into tjson (jsoncol) VALUES ( '{"y" : "4", "n" : "0" , "default" : "4"}');
PL/SQL块
DECLARE
v_jsoncol tjson.jsoncol%TYPE;
v_json_obj json_object_t;
v_new_jsoncol tjson.jsoncol%TYPE;
BEGIN
SELECT
jsoncol
INTO v_jsoncol
FROM
tjson; --use appropriate where clause
v_json_obj := TREAT(json_element_t.parse(v_jsoncol) AS json_object_t);
v_json_obj.put('n',2); --modifies the value
v_new_jsoncol := v_json_obj.to_string; --converts to string
UPDATE tjson
SET
jsoncol = v_new_jsoncol; --update ( use appropriate where clause)
END;
/
修改的数据
select jsoncol FROM tjson;
JSONCOL
--------------------------------------------------------------------------------
{"y":"4","n":2,"default":"4"}
编辑:
启动Oracle 19c时,您可以使用
在Oracle 18c的纯SQL(例如,一个简单的UPDATE语句,从JSON值中删除一个特定的键)中,有什么方法可以做到这一点吗?@a_horse:我找不到。很高兴知道是否有。
select json_mergepatch (jsoncol, '{"n" : "2"}') as new_json from tjson;
NEW_JSON
{
"y" : "4",
"n" : "2",
"default" : "4"
}