To replicate this issue you can do the following in APEX:
1- Create an On Demand Application Process
-- AP_JSON_TEST
DECLARE
v_sql VARCHAR2 (4000);
BEGIN
-- Note: This query is meant to return no rows
v_sql := 'SELECT ename FROM emp WHERE 1 = 2';
-- Print JSON result set
apex_util.json_from_sql (v_sql);
END;
2- Run the following JS code(easiest using firebug)
var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=AP_TODO_DEL',$v('pFlowStepId'));
vReturn = get.get();
At this point if you're using FireBug you'll notice that "sqlerrm:ORA-06502: PL/SQL: numeric or value error" now appears in the console window.
At first glance it appears that you have something wrong with your Application Process or with your query. After some digging around I finally realized that it's a bug with APEX_UTIL.JSON_FROM_SQL.
The good news is that it's really easy to fix. All you need to do is catch any exceptions and return a JSON object with no rows:
-- AP_JSON_TEST
DECLARE
v_sql VARCHAR2 (4000);
BEGIN
-- Note: This query is meant to return no rows
v_sql := 'SELECT ename FROM emp WHERE 1 = 2';
-- Print JSON result set
apex_util.json_from_sql (v_sql);
-- *** FIX ***
EXCEPTION
WHEN OTHERS THEN
HTP.p ('{"row":[]}');
END;
Good catch! Thanks!
ReplyDeleteI am used to wrap all apex_util functions so those bugs won't affect me too much.
So what is the bug exactly?
ReplyDeleteYou got a 6502 but don't explain why it happened. Is it only when the SQL-Statement returns no rows?
Peter
Martin,
ReplyDeleteIt should either raise a NO_DATA_FOUND exception or return NULL and it should be documented either way. An ORA-06502 exception doesn't make much sense... Good catch.
Regards,
Dan
Hi Peter,
ReplyDeleteThe ORA-06502 error is occurring when no rows are returned in the query. As Dan mentioned the ORA-06502 doesn't make much sense for NO_DATA_FOUND...
Martin
Hi Martin,
ReplyDeleteI have filed a bug for that problem.
Regards
Patrick
Has anyone found a way to actually return the JSON into a variable, rather than print it? I wanted to send the JSON to another app via an HTTP POST, but json_from_sql() strangely doesn't return a value.
ReplyDeleteHi Chris,
ReplyDeletejson_from_sql() returns a JSON object representing the data set from the query. You can then send the JSON object to another application if necessary.
Martin
Hello Everyone,
ReplyDeletePlease help me. When I try to see the reports, applying Filter option. I can see the proper output. But as soon as I download the filtered report in CSV format. The Downloaded report has no effect of filter. Please help me out of this.
Raj
Hi Raj,
ReplyDeleteThis may be a good topic to post on the APEX forums or http://apex.stackexchange.com
Martin