ORACLE 获取表中JSON字段中的VALUE值自定义函数 |
您所在的位置:网站首页 › 获取json字符串中的值函数 › ORACLE 获取表中JSON字段中的VALUE值自定义函数 |
CREATE OR REPLACE function get_json( v_table varchar2,v_field varchar2,v_key varchar2)-- 1:表,2:json所在字段,3:期望获取value的key值
return varchar2
as
v_res varchar2(100); --返回值
v_json varchar2(4000); --json
v_sql varchar2(1000); -- 自定义SQL语句
v_qswz number; -- value值起始位置
v_jswz number; -- value值结束位置
-- regexp_instr(值,要匹配的格式,从第几位开始(缺省时为从第一位开始)),如:select regexp_instr(‘{"id":"1234"}’,'id') from dual; 结果为:3
begin
v_sql := 'select '||v_field|| ' from ' || v_table;
execute immediate v_sql into v_json;
--起始位置:满足key字符串的位置+key长度+2;
select regexp_instr(v_json, v_key)+ 2 +length(v_key) into v_qswz from dual;
if regexp_instr(v_json, v_key)-2 != 1 then
v_qswz :=v_qswz +1;
end if ;
--结束位置:起始位置到下一个‘","’(或"})的位置;
select regexp_instr(v_json, '","',v_qswz) into v_jswz from dual;
if regexp_instr(v_json, v_key)-2 != 1 and v_jswz !=0 then
v_jswz :=v_jswz-1;
end if;
if v_jswz = 0 then
v_jswz := length(v_json)-1;
end if;
select substr(v_json, v_qswz, v_jswz-v_qswz) into v_res from dual;
select replace(v_res,'"','') into v_res from dual;
return v_res;
end;
以下为进阶版: 1.创建数据对象,用于保存结果集中的结果。 2.创建类表对象,用于保存要返回的结果。 3.创建函数。整体代码如下: create or replace type ecs_json_type as object ( json_value VARCHAR2(100) ); create or replace type ecs_json_value is table of ecs_json_type; CREATE OR REPLACE function ecs_get_json_values(v_table varchar2, v_field varchar2, v_key varchar2, v_condition varchar2, v_conditionValue varchar2) return ecs_json_value as v_res varchar2(100); --返回值 v_json varchar2(4000); --json v_sql varchar2(1000); -- 自定义SQL语句 v_qswz number; -- value值起始位置 v_jswz number; -- value值结束位置 rs ecs_json_value := ecs_json_value(); type ref_cursor is ref cursor; v_cursor ref_cursor; -- regexp_instr(值,要匹配的格式,从第几位开始(缺省时为从第一位开始)),如:select regexp_instr(‘{"id":"1234"}’,'id') from dual; 结果为:3 begin v_sql := 'select ' || v_field || ' from ' || v_table || ' where ' || v_condition || '=''' || v_conditionValue || ''''; open v_cursor for v_sql; loop fetch v_cursor into v_json; exit when v_cursor%notfound; --起始位置:满足key字符串的位置+key长度+2; select regexp_instr(v_json, v_key) + 2 + length(v_key) into v_qswz from dual; if regexp_instr(v_json, v_key) - 2 != 1 then v_qswz := v_qswz + 1; end if; --结束位置:起始位置到下一个‘","’(或"})的位置; select regexp_instr(v_json, '","', v_qswz) into v_jswz from dual; if regexp_instr(v_json, v_key) - 2 != 1 and v_jswz != 0 then v_jswz := v_jswz - 1; end if; if v_jswz = 0 then v_jswz := length(v_json) - 1; end if; select substr(v_json, v_qswz, v_jswz - v_qswz) into v_res from dual; select replace(v_res, '"', '') into v_res from dual; rs.extend; rs(rs.count) := ecs_json_type(v_res); end loop; close v_cursor; return rs; end;运行方式为: select * from table(ecs_get_json_values('v_table','v_field','v_key','v_condition','v_conditionValue')); 其中:1:表,2:json所在字段,3:期望获取value的key值 4:条件字段 5:条件字段值 例:test表中,a字段为json格式,{“fwbh”:123,"fwdz":"伦敦"},共10行数据, 1.如需获取前5行数据,需要更改函数中where ' || v_condition || '=''' || v_conditionValue ||部分,将=换为< ,并需要利用rownum; 语句:select * from table(ecs_get_json_values('test','a','fwdz','rownum','5')); 2.如需查询全表,则将函数后两位参数写为1;即select * from table where 1 = 1; 语句:select * from table(ecs_get_json_values('test','a','fwdz','1','1')); 在此感谢MannixFan的支持 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |