博客地址取名为:dreamhui,我最喜欢的一个英文网名。
先记录下最近写的一段pl/Sql函数吧,里面用到了pl/json包。
--总函数
create or replace function f_all(p_json varchar2,p_server varchar2) return clob is
v_json_ret clob;
begin
--调用方式:SELECT f_all('{"dept": "10" }','f_eop_empjson') FROM dual;
if p_server = 'f_eop_empjson' then
select f_empjson(p_json) into v_json_ret from dual;
elsif p_server = 'f_eop_json_others' then
v_json_ret := 'ret_others';
end if;
return v_json_ret;
end;
--服务函数
create or replace function f_empjson(p_json varchar2) return clob is
v_dept varchar2(10);
v_json_ret clob;
obj json;
begin
--'{"dept": "30" }'
obj := json(p_json);--解析JSON
v_dept := json_ext.get_string(obj,'dept');--获取JSON
v_json_ret := '[';
for rc_json in (
SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno FROM emp WHERE deptno = v_dept or length(v_dept) is null
)loop
v_json_ret:= v_json_ret ||'{"empno":"'||rc_json.empno||'","ename":"'||rc_json.ename||'","job":"'||rc_json.job||'","mgr":"'||rc_json.mgr||'","hiredate":"'||rc_json.hiredate||'","sal":"'||rc_json.sal||'","comm":"'||rc_json.comm||'","deptno":"'||rc_json.deptno||'"},';
end loop;
v_json_ret := substr(v_json_ret,1,length(v_json_ret)-1)|| ']';
return v_json_ret;
end;