我在更新 clob 数据类型内的某些值时遇到一些问题。 我正在分享一些关于我的情况的代码。请指导我有没有办法写一些更新声明来更新这个?
CREATE TABLE TEST_CLOB
( ID NUMBER(10,0),
IMPORTDATA CLOB
);
insert into test_clob values (1001,'{"ClassId":30074,"Attributes":[{"Name":"ID","Value":"265794"},{"Name":"HREF","Value":"-1"},{"Name":"HPRECISION","Value":"5"},{"Name":"HMETHOD","Value":"96"},{"Name":"GEO-METHOD","Value":"96"},{"Name":"GEO-PRECISION","Value":"5"},{"Name":"GEO-VISIBILITY","Value":"0"},{"Name":"REMARK","Value":"-1"}]}');
insert into test_clob values (1002,'{"ClassId":30000,"Attributes":[{"Name":"ID","Value":"265756"},{"Name":"HREF","Value":"unknown"},{"Name":"HPRECISION","Value":"4"},{"Name":"HMETHOD","Value":"90"},{"Name":"GEO-METHOD","Value":"0"},{"Name":"GEO-PRECISION","Value":"1"},{"Name":"GEO-VISIBILITY","Value":"0"},{"Name":"REMARK","Value":"-1"}]}');
SELECT id,HMETHOD,HPRECISION, HREF
FROM (
select id,
name,
value
from test_clob tt1
CROSS APPLY JSON_TABLE
(
tt1.IMPORTDATA,
'$'
COLUMNS
(
classid NUMBER PATH '$.ClassId',
NESTED PATH '$.Attributes[*]' COLUMNS
(
name VARCHAR2(20) PATH '$.Name',
value VARCHAR2(20) PATH '$.Value'
)
)
)
)
PIVOT
(
MAX(value) FOR name IN
(
'HMETHOD' AS HMETHOD,
'HPRECISION' AS HPRECISION,
'HREF' AS HREF
)
);
这里可以显示我上面sql中的数据。我给出的是从小提琴中截取的屏幕截图。
现在我的问题是将整个表的 HREF 值从 “未知” 更新为 -1。
如果有人需要使用那里,我也会提供小提琴链接。
with data(id, js) as (
select 1001, '{"ClassId":30074,"Attributes":[{"Name":"ID","Value":"265794"},{"Name":"HREF","Value":"-1"},{"Name":"HPRECISION","Value":"5"},{"Name":"HMETHOD","Value":"96"},{"Name":"GEO-METHOD","Value":"96"},{"Name":"GEO-PRECISION","Value":"5"},{"Name":"GEO-VISIBILITY","Value":"0"},{"Name":"REMARK","Value":"-1"}]}' union all
select 1002, '{"ClassId":30000,"Attributes":[{"Name":"ID","Value":"265756"},{"Name":"HREF","Value":"unknown"},{"Name":"HPRECISION","Value":"4"},{"Name":"HMETHOD","Value":"90"},{"Name":"GEO-METHOD","Value":"0"},{"Name":"GEO-PRECISION","Value":"1"},{"Name":"GEO-VISIBILITY","Value":"0"},{"Name":"REMARK","Value":"-1"}]}'
)
select id,
json_transform(js,
REPLACE '$.Attributes[*]?(@.Value == "unknown").Value' = '-1'
) as new_js
from data d
;
声明 v_clob CLOB; 开始 -- 选择现有的 CLOB 值到变量中 选择导入数据到 v_clob 来自 TEST_CLOB 其中 ID = 1001 更新;
-- 将数据追加到 CLOB DBMS_LOB.APPEND(v_clob, ',{"名称":"NEW_ATTR","值":"new_value"}]}');
-- 更新表中的 CLOB 更新测试_CLOB 设置导入数据 = v_clob 其中 ID = 1001;
承诺; 结束;