我的Oracle(v12.2)表中有一个CLOB字段,如下所示:
CREATE TABLE dmo_person (
per_id RAW(16) CONSTRAINT NN_per_id NOT NULL,
per_name VARCHAR2(128),
per_tags CLOB CONSTRAINT dmo_pers_json_0 CHECK (per_tags IS JSON),
CONSTRAINT sko_person_pk_0 PRIMARY KEY (per_id)
);
JSON数据具有以下结构:
insert into dmo_person
( per_id, per_name, per_tags )
values
(
sys_guid(),
'John Doe',
'{ "perm_admin" : 1, "perm_fileuser" : 0, "perm_subcon" : 1} ',
);
所以我的问题:使用SQL更新语句,如何向我的CLOB添加另一个值"perm_bigboss" : 1
?是否有一种使用SQL设置单个值"perm_admin" : 0
的简单方法?
答案 0 :(得分:0)
答案 1 :(得分:0)
对于Oracle 11g版本,不支持Json操作。因此,我们必须使用基本功能:SUBSTR / INSTR / SUBSTR
create OR replace FUNCTION Get_Value(data_request VARCHAR2, s_key VARCHAR2) RETURN VARCHAR2
IS s_Value VARCHAR2(2000);
index1 integer := 0;
index2 integer := 0;
BEGIN
s_Value := '';
SELECT INSTR(data_request, '"' || s_key || '":"' , 1, 1) INTO index1 FROM dual;
IF (index1 > 0) THEN
index1 := index1 + LENGTH('"' || s_key || '":"') ;
s_Value := SUBSTR(data_request, index1, LENGTH(data_request));
SELECT INSTR(s_Value, '"', 1, 1) INTO index2 FROM dual;
s_Value := SUBSTR(data_request, index1, index2-1);
END IF;
RETURN(s_Value);
END;
假设,我们有一个表'tableName',其表列为'data_request,例如:{“ ip”:“ 127.0.0.1”,“ username”:“ userTest”}
然后我们可以像这样解析json字符串:
SELECT data_request,
Get_Value(data_request, 'numeroDeCompte'), Get_Value(data_request, 'ip') ip,
Get_Value(data_request, 'username') username
FROM tableName