Oracle database 12c : ORACLE JSON QUERY SEARCH

社会主义新天地 提交于 2020-01-04 05:04:33

问题


Creating two types of table to store JSON values (BLOB and NCLOB) and my aim to search the required values from JSON table;

Code Snippet 1: Creating table with one column as NCLOB.

create table departments_json_nclob (
  department_id   integer not null primary key,
  department_data NCLOB not null
);

Simple insert with multibyte character (that is value : məharaːʂʈrə):

insert into departments_json_nclob 
  values ( 200,'{"department_list":[{"Deptname":"DEPT-A", "value" : "məharaːʂʈrə"}]}'); 

Code Snippet 2: Now, I have created one other table with BLOB datatype:

create table departments_json (
  department_id   integer not null primary key,
  department_data blob not null
);

Added constraint to allow only JSON

alter table departments_json
  add constraint dept_data_json 
  check ( department_data is JSON FORMAT JSON STRICT );

Insert normal JSON

insert into departments_json 
  values ( 100, utl_raw.cast_to_raw ('{"department_list":[{"Deptname":"DEPT-A", "value" : "məharaːʂʈrə"}]}'));

Insertion Verified from below query:

SELECT json_value(department_data format json, '$.department_list.value'  ) 
FROM departments_json JS 
WHERE DEPARTMENT_ID=100;

output is: məharaːʂʈrə

Now, I will have one more insertion in same table i.e. 'departments_json' but this time I will take required insertion value from NCLOB table departments_json_nclob:

declare

    i nclob;

begin

    select department_data into i from departments_json_nclob where department_id =200;

    --inserting same way as I inserted in departments_json for department_id 100 but value comes from NCLOB

    insert into departments_json 
    values ( 101, utl_raw.cast_to_raw (i));

    commit;
end;

Again, insertion verified with below query:

SELECT json_value(department_data format json, '$.department_list.value'  ) 
FROM departments_json JS 
WHERE DEPARTMENT_ID=101;

output is: məharaːʂʈrə

Now my question is:

When I search for multibyte character, query return result of one query only that is direct insertion in BLOB table. Which is DEPARTMENT_ID=100 - why not 101?

Below query:

SELECT *
FROM   departments_json
WHERE JSON_value(department_data format json, '$.department_list.value') = ('məharaːʂʈrə');

SELECT *
FROM   departments_json
WHERE JSON_TEXTCONTAINS(department_data,  '$.department_list.value', 'məharaːʂʈrə')

below query shows which character are multibyte:

select c, length(c), lengthb(c) 
from ( select substr(s, level, 1) c 
       from ( select 'məharaːʂʈrə' s 
              from dual) 
       connect by level <= length(s));

来源:https://stackoverflow.com/questions/58909775/oracle-database-12c-oracle-json-query-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!