Related
I am trying to convert a json array to json clob and the convert it object type in an Oracle stored procedure.
Below is the object type I have in Oracle.
create or replace TYPE REPORT_OBJ FORCE as OBJECT (
id NUMBER,
name NUMBER,
createDt Date,
value NUMBER(10,2)
);
create or replace TYPE REPORT_OBJ_LIST as TABLE OF REPORT_OBJ;
This is my json array:
[{"id":1,"name":"john",:"createDt":"01-jan-2020","value":10},
{"id":2,"name":"crystal","createDt":"01-feb-2020","value":20},
{"id":3,"name":"bob","createDt":"01-mar-2020","value":30}]
This is my stored procedure which takes report_obj_list as input parameter
create or replace PROCEDURE SaveUpdate_ReportsData(reportList IN REPORT_OBJ_LIST)
AS v_count number;
v_column REPORTS_DATA.id%TYPE;
updatedRecs Number;
recsCount Number;
dbid REPORTS_DATA.Id%TYPE;
dbname REPORTS_DATA.name%TYPE;
dbcreateDt REPORTS_DATA.createDt%TYPE;
dbvalue REPORTS_DATA.value%TYPE;
BEGIN
recsCount := 0;
updatedRecs := 0;
for i in reportList.first..reportList.last loop
v_column := 0;
dbid := 0;
dbname := 0;
dbcreateDt := sysdate;
dbvalue := 0;
BEGIN
SELECT DISTINCT NVL(b.repId,0) into v_column from (
(SELECT 'TEMP' as temp from REPORTS_DATA) a left join (
SELECT DISTINCT 'TEMP' AS temp, NVL(id,0) as repId FROM REPORTS_DATA
where createDt = reportList(i).createDt ) b on a.temp = b.temp);
if(v_column <= 0 ) then
INSERT INTO REPORTS_DATA (Id,name,createDt,value)
VALUES (reportList(i).Id,reportList(i).name, reportList(i).createDt,
reportList(i).value);
updatedRecs := updatedRecs+1;
else
updatedRecs := updatedRecs+1;
SELECT id,name,createDt,value INTO
dbid,dbname,dbcreateDt,dbvalue
FROM REPORTS_DATA
where createDt = reportList(i).createDt;
update REPORTS_DATA set id = NVL(reportList(i).id,dbid),
name = NVL(reportList(i).name,dbname) ,
createDt = NVL(reportList(i).createDt,dbcreateDt),
value = NVL(reportList(i).value, dbvalue);
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_column := null;
DBMS_OUTPUT.PUT_LINE('hello' || v_column);
END;
end loop;
Commit;
recsCount:= updatedRecs ;
DBMS_OUTPUT.PUT_LINE('HELOOwq ' || recsCount);
end SaveUpdate_ReportsData ;
below is the oracle table
create table REPORTS_DATA(
id number,
name varchar(200),
createdt date,
value number(10,2)
);
From java, I have to convert jsonarray as clob (so that it can accept large amount of data as input to stored procedure), and the stored procedure should accept json array of clob and convert it to 'Report_obj_list', and from there the existing stored procedure will work fine. I have written the stored procedure which accepts object but i need to make changes so that it accepts clob json array and converts that to object inside the stored procedure.
Updated stored procedure
create or replace PROCEDURE SaveUpdate_ReportsData(intnum in Number)
AS v_count number;
jstr clob;
reportList report_obj_list;
v_column REPORTS_DATA.id%TYPE;
dbid REPORTS_DATA.Id%TYPE;
dbname REPORTS_DATA.name%TYPE;
dbcreateDt REPORTS_DATA.createDt%TYPE;
dbvalue REPORTS_DATA.value%TYPE;
BEGIN
jstr := to_clob('[{"id":1,"name":"john","createDt":"01-jan-2020","value":10},
{"id":2,"name":"crystal","createDt":"01-feb-2020","value":20},
{"id":3,"name":"bob","createDt":"01-mar-2020","value":30}]');
select report_obj(id, name, to_date(createdt, 'dd-mon-yyyy'), value)
bulk collect into reportList
from json_table(jstr, '$[*]'
columns( id number path '$.id',
name varchar2(20) path '$.name',
createdt varchar2(11) path '$.createDt',
value number(10, 2) path '$.value'
)
);
for i in reportList.first..reportList.last loop
DBMS_OUTPUT.PUT_LINE('name_ ' || reportList(i).name);
v_column := 0;
dbid := 0;
dbname := 0;
dbcreateDt := sysdate;
dbvalue := 0;
BEGIN
SELECT DISTINCT NVL(b.repId,0) into v_column from (
(SELECT 'TEMP' as temp from REPORTS_DATA) a left join (
SELECT DISTINCT 'TEMP' AS temp, NVL(id,0) as repId FROM REPORTS_DATA
where createDt = reportList(i).createDt ) b on a.temp = b.temp);
if(v_column <= 0 ) then
INSERT INTO REPORTS_DATA (Id,name,createDt,value)
VALUES (reportList(i).Id,reportList(i).name, reportList(i).createDt,
reportList(i).value);
else
SELECT id,name,createDt,value INTO
dbid,dbname,dbcreateDt,dbvalue
FROM REPORTS_DATA
where createDt = reportList(i).createDt;
update REPORTS_DATA set id = NVL(reportList(i).id,dbid),
name = NVL(reportList(i).name,dbname) ,
createDt = NVL(reportList(i).createDt,dbcreateDt),
value = NVL(reportList(i).value, dbvalue);
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_column := null;
END;
end loop;
Commit;
end SaveUpdate_ReportsData ;
and i am calling the stored procedure as below:
DECLARE
BEGIN
SaveUpdate_ReportsData(
12
);
END;
/
It's not throwing any type of error, but at the same time its not inserting the data into the REPORTS_DATA table not even printing the name.
Help me solve the problem.
Thanks in advance.
Here is how you can extract the data from a JSON (in CLOB data type) and pass it to a collection (nested table) of objects of user-defined type, like you have. The PL/SQL code is for a function that accepts a CLOB (assumed to be valid JSON) and returns a nested table of objects. (Then I show one example of invoking the function in SQL, to see what was saved in it.)
Not sure what you mean by converting your JSON array to CLOB. As far as Oracle is concerned, the JSON is a CLOB.
Anyway - here is the function:
create or replace type report_obj force as object (
id number,
name_ varchar2(20),
createdt date,
value_ number(10,2)
);
/
create or replace type report_obj_list as table of report_obj;
/
create or replace function json_to_obj_list (jstr clob)
return report_obj_list
as
lst report_obj_list;
begin
select report_obj(id, name_, to_date(createdt, 'dd-mon-yyyy'), value_)
bulk collect into lst
from json_table(jstr, '$[*]'
columns( id number path '$.id',
name_ varchar2(20) path '$.name',
createdt varchar2(11) path '$.createDt',
value_ number(10, 2) path '$.value'
)
);
return lst;
end;
/
(As you can see, I changed your object type definition - I changed the attribute names name and value to name_ and value_, because name and value are Oracle keywords so they shouldn't be used as identifiers.)
And here is how this works. Note that I am passing an explicit CLOB to the function. More likely, you will want to store your CLOBs somewhere (table?) and pass them from there. That part is relatively trivial.
select * from json_to_obj_list(
to_clob(
'[{"id":1,"name":"john","createDt":"01-jan-2020","value":10},
{"id":2,"name":"crystal","createDt":"01-feb-2020","value":20},
{"id":3,"name":"bob","createDt":"01-mar-2020","value":30}]')
);
ID NAME_ CREATEDT VALUE_
---------- -------------------- ----------- ----------
1 john 01-jan-2020 10
2 crystal 01-feb-2020 20
3 bob 01-mar-2020 30
Note that createdt is in fact date data type; in the output it looks like your inputs only because I intentionally set my nls_date_format to match it. Otherwise your query will return the dates in that column in whatever format is the default for your session.
I need to receive a json string as a parameter of the procedure and parse it as a json object.
An example of this json string: {'new_settings': [{'setting_name': 'test', 'setting_value': 'test_value'}]}
I need to fetch the array property named "new_settings" and iterate through the objects inside this array.
This is what i have at the moment:
CREATE PROCEDURE `sp_test` (IN `_settings` longtext)
BEGIN
DECLARE i INT UNSIGNED DEFAULT 0;
DECLARE count INT UNSIGNED DEFAULT 0;
DECLARE current_item LONGTEXT;
START TRANSACTION;
SET count = (JSON_LENGTH(JSON_EXTRACT(_settings, '$.new_settings')) - 1);
WHILE i < count DO
SET current_item := (_settings, CONCAT('$[', i, ']'));
INSERT INTO settings(setting_name, setting_value) VALUES (JSON_EXTRACT(current_item, '$.setting_name'), JSON_EXTRACT(current_item, '$.setting_value'));
SET i := i + 1;
END WHILE;
COMMIT;
END
But i am getting the following error: "Syntax error in JSON text in argument 1 to function 'json_extract' at position 3" and i dont understand what is wrong.
Can you provide some insight?
Thank you in advance.
Your json is all wrong.
You always should check with json Validators, if mysql can read your input.
SET #a = '{"new_settings": [{"setting_name": "test", "setting_value": "test_value"}]}';
SELECT JSON_EXTRACT(#a, '$.new_settings[0].setting_name') c1
, JSON_EXTRACT(#a, '$.new_settings[0].setting_value') c2;
Would give you
c1 c2
test test_value
Where did you get your json String?
I'm trying to extract a very long string into clob from json_object_t and got some weird database behaviour (12.2c) with json_object_t.get_clob(key) method.
There is a sample code than does following:
DECLARE
l_data CLOB := '{"text": "very long string about 1M chars"}';
l_json json_object_t;
l_text CLOB := EMPTY_CLOB();
BEGIN
l_json := json_object_t.parse(l_data);
l_text := l_json.get_clob('text');
dbms_output.put_line('got ' || dbms_lob.getlength(l_text) || ' chars');
END;
When string length in a 'text' key is less than 32k chars, get_clob method works just fine and shows appropriate result, but with longer strings it produces an empty clob with zero length, just like get_string, but without 'character string buffer too small' exception.
I've tried to get same data via json_table query, but it cannot extract data to clob column at all, only varchar/number is allowed.
Is that a bug or am I doing something wrong? Is there any other ways to extract long strings from JSON keys?
I work with Oracle Database JSON Store group and would be happy to assist you with this issue you're facing. Could you try the alternate get_Clob procedure instead of this function and tell us what the behavior is?
Signature:
MEMBER PROCEDURE get_Clob(key VARCHAR2, c IN OUT CLOB)
Please try this:
DECLARE
content_json CLOB := '{"value":"';
content_json_end CLOB := '"}';
content_tmp CLOB := 'ab';
l_json json_object_t;
l_text CLOB := EMPTY_CLOB();
tmp clob;
BEGIN
-- 13 gives 16K
-- 14 gives 32K
FOR count IN 1 .. 14
loop
dbms_lob.append(content_tmp, content_tmp); -- a bad append for now
END loop;
dbms_lob.append(content_json, content_tmp);
dbms_lob.append(content_json, content_json_end);
l_json := json_object_t.parse(content_json);
l_json.get_clob('value', l_text); -- !!! TRY THIS PROC get_Clob
--l_text := l_json.get_clob('value');
dbms_output.put_line('Lob size in Kb: ');
dbms_output.put_line(dbms_lob.getLength(l_text) / 1024);
END;
/
Looking forward to your findings..
This works as well. Instead using the the get_clob method, use c:
DECLARE
CURSOR crsrJSON IS
SELECT
json_object( 'employee_id' VALUE employee_id,
'first_name' VALUE first_name,
'last_name' VALUE last_name,
'email' VALUE email,
'phone_number' VALUE phone_number,
'hire_date' VALUE to_char(hire_date,'MM/DD/YYYY'),
'job_id' VALUE job_id,
'salary' VALUE nvl(salary,0),
'commission_pct' VALUE nvl(commission_pct,0),
'manager_id' VALUE NVL(manager_id,0),
'department_id' VALUE NVL(department_id,0),
'department_name' VALUE (select department_name from departments x where x.department_id = hr.department_id),
'job_title' VALUE (select job_title from jobs x where x.job_id = hr.job_id)) emp_data
FROM
employees hr;
js_array JSON_ARRAY_T := new JSON_ARRAY_T;
json_obj JSON_OBJECT_T := JSON_OBJECT_T();
json_clob CLOB := EMPTY_CLOB();
BEGIN
FOR data_rec IN crsrJSON LOOP
js_array.append(JSON_ELEMENT_T.parse(data_rec.emp_data));
END LOOP;
json_obj.put('data',js_array);
IF json_obj.has('data') THEN
json_clob := json_obj.to_clob;
DBMS_OUTPUT.PUT_LINE(json_clob);
ELSE
DBMS_OUTPUT.PUT_LINE('Nope');
END IF;
END;
with data as
( select
xmlelement(e,regexp_replace('{"name":"'||colname||'"}', '[[:cntrl:]]', ''),',') col1
from tblname
)
select
rtrim(replace(replace(replace(xmlagg(col1).getclobval(),'&'||'quot;','"'),'<E>',''),'</E>',''),',')
as very_long_json
from data;
FORALL update is updating only last record from collection and it's giving error ORA-22160: element at index [1] does not exist. I have scenario were i have to update million of records. When i execute the below code the update is happening only for the 100th record remaining 99 records remains same( i tested with 100 records). after execution it's giving ORA-22160: element at index [1] does not exist. i am using oracle 9i database.need suggestion for this issue.
Declare
CURSOR tk_iflow_cur
IS
SELECT DISTINCT top.rule_id,
top.rule_item_id,
msib.segment2
FROM iflow_rules top,
iflow_active_rules msib
WHERE top.rule_id = msib.rule_item_id
AND msib.organization_id = 5039 and rownum<=100
ORDER BY top.ora_inv_item_id; -- cursor fetches 100 row
---Variable declaration
TYPE l_iflow_id
IS
TABLE OF VARCHAR2(200) INDEX BY BINARY_INTEGER;
TYPE l_iflow_org
IS
TABLE OF NUMBER INDEX BY BINARY_INTEGER;
TYPE l_iflow_inv_id
IS
TABLE OF NUMBER INDEX BY BINARY_INTEGER;
TYPE l_r12_inv_id
IS
TABLE OF NUMBER;
TYPE l_r12_item_key
IS
TABLE OF VARCHAR2(200) INDEX BY BINARY_INTEGER;
TYPE l_r12_iflow
IS
TABLE OF VARCHAR2(200) INDEX BY BINARY_INTEGER;
TYPE l_omar_item_id
IS
TABLE OF NUMBER INDEX BY BINARY_INTEGER;
TYPE l_omar_seg2
IS
TABLE OF VARCHAR2(200) INDEX BY BINARY_INTEGER;
tk_iflow_id l_iflow_id;
tk_iflow_org l_iflow_org;
tk_iflow_inv_id l_iflow_inv_id;
t_omar_item_id l_omar_item_id;
t_omar_seg2 l_omar_seg2;
r12_inv_id l_r12_inv_id:=l_r12_inv_id();
r12_item_key l_r12_item_key;
r12_iflow l_r12_iflow;
user_excep EXCEPTION;
v_err_count NUMBER;
PRAGMA EXCEPTION_INIT (user_excep, -24381);
r12_item_id2 VARCHAR2(200);
BEGIN --
tk_iflow_id.DELETE;
tk_iflow_inv_id.DELETE;
OPEN tk_iflow_cur;
LOOP --Cursor Loop
tk_iflow_id.DELETE;
tk_iflow_inv_id.DELETE;
t_omar_seg2.DELETE;
FETCH tk_iflow_cur BULK COLLECT INTO tk_iflow_id, tk_iflow_inv_id, t_omar_seg2; ---100 records assigned to variables
FOR i IN 1..tk_iflow_id.COUNT -- to store the cursor value
LOOP --for Loop
BEGIN
--Comment : passing the cursor value to derive new records from different instance for updating the records
SELECT DISTINCT segment1,
inventory_item_id,
item_type
INTO r12_iflow(i),
r12_item_id2,
r12_item_key(i)
FROM iflow.ifl_active_rules#R12_db_link
WHERE segment1 =tk_iflow_id(i)
AND NVL(SEGMENT2,'NULL') = NVL(t_omar_seg2(i),'NULL')
AND organization_id =5063;
EXCEPTION
WHEN OTHERS THEN
r12_inv_id(i) := 0;
r12_item_key(i) := 0;
END;
r12_inv_id :=l_r12_inv_id();
r12_inv_id.EXTEND (i);
r12_inv_id (i) := r12_item_id2;
END LOOP; --end for loop
BEGIN
FORALL i IN r12_inv_id.FIRST..r12_inv_id.LAST SAVE EXCEPTIONS -- for all the derived records i'm updating the target table.100 records i'm updating
UPDATE iflow_active_rules
SET ora_org_id =5063,
ora_inv_item_id =r12_inv_id(i)
WHERE iflow_id =r12_iflow(i)
AND ora_inv_item_id = tk_iflow_inv_id(i);
EXCEPTION
WHEN user_excep THEN
v_err_count := SQL%BULK_EXCEPTIONS.COUNT;
FOR i IN 1 .. v_err_count
LOOP
DBMS_OUTPUT.put_line ( 'Error: ' || i || ' Array Index: ' || SQL%BULK_EXCEPTIONS (i).ERROR_INDEX || ' Message: ' || SQLERRM (SQL%BULK_EXCEPTIONS (i).ERROR_CODE) );
END LOOP;
END;
BEGIN
FORALL i IN r12_inv_id.FIRST .. r12_inv_id.LAST SAVE EXCEPTIONS
UPDATE iflow_rules --- updating openup system iflow cost details
SET ora_org_id =5063,
ora_inv_item_id =r12_inv_id(i)
WHERE iflow_id =r12_iflow(i)
AND ora_inv_item_id = tk_iflow_inv_id(i) ;
EXCEPTION
WHEN user_excep THEN
v_err_count := SQL%BULK_EXCEPTIONS.COUNT;
FOR i IN 1 .. v_err_count
LOOP
DBMS_OUTPUT.put_line ( 'Error: ' || i || ' Array Index: ' || SQL%BULK_EXCEPTIONS (i).ERROR_INDEX || ' Message: ' || SQLERRM (SQL%BULK_EXCEPTIONS (i).ERROR_CODE) );
END LOOP;
END;
EXIT
WHEN tk_iflow_id.COUNT=0;
COMMIT;
END LOOP; --End Cursor Loop
CLOSE tk_iflow_cur;
END;
This is happening because you are using r12_iflow collection in the scope of collection r12_inv_id where it not not accessible. FORALL statement is differnt the FOR LOOP. Even though they work pretty on same logic but they differs on scope of usage of variables. You can create a RECORD in your case and do the processing. See below how you can do it. PS Not tested.
DECLARE
CURSOR tk_iflow_cur
IS
SELECT DISTINCT top.rule_id, top.rule_item_id, msib.segment2
FROM iflow_rules top, iflow_active_rules msib
WHERE top.rule_id = msib.rule_item_id
AND msib.organization_id = 5039
AND ROWNUM <= 100
ORDER BY top.ora_inv_item_id; -- cursor fetches 100 row
Type var_cur is table of tk_iflow_cur%rowtype index by pls_integer;
l_cur var_cur;
--Record of your variables.
TYPE var_rec is RECORD
(
l_r12_iflow VARCHAR2 (200),
l_r12_inv_id number,
l_r12_item_key VARCHAR2 (200)
);
TYPE rec is table of var_rec index by pls_integer;
--Variable declared to hold the result of your cursor.
l_rec rec;
user_excep EXCEPTION;
v_err_count NUMBER;
PRAGMA EXCEPTION_INIT (user_excep, -24381);
r12_item_id2 VARCHAR2 (200);
BEGIN
OPEN tk_iflow_cur;
LOOP --Cursor Loop
FETCH tk_iflow_cur BULK COLLECT INTO l_cur LIMIT 100; ---100 records assigned to variables
FOR i IN 1 .. l_cur.COUNT -- to store the cursor value
LOOP --for Loop
BEGIN
--Comment : passing the cursor value to derive new records from different instance for updating the records
SELECT DISTINCT segment1, inventory_item_id, item_type
INTO l_rec(i).l_r12_iflow ,l_rec(i).l_r12_inv_id ,l_rec(i).l_r12_item_key
FROM iflow.ifl_active_rules#R12_db_link
WHERE segment1 = l_cur(i).rule_id
AND NVL (SEGMENT2, 'NULL') = NVL (l_cur(i).segment2 , 'NULL')
AND organization_id = 5063;
EXCEPTION
WHEN OTHERS
THEN
l_rec(i).l_r12_iflow := 0;
l_rec(i).l_r12_inv_id:= 0;
l_rec(i).l_r12_item_key := '';
END;
END LOOP; --end for loop
BEGIN
FORALL i IN 1 .. l_rec.COUNT SAVE EXCEPTIONS -- for all the derived records i'm updating the target table.100 records i'm updating
UPDATE iflow_active_rules
SET ora_org_id = 5063,
ora_inv_item_id = l_rec(i).l_r12_inv_id
WHERE iflow_id = l_rec(i).l_r12_iflow
AND ora_inv_item_id = l_rec(i).l_r12_inv_id;
EXCEPTION
WHEN user_excep
THEN
v_err_count := SQL%BULK_EXCEPTIONS.COUNT;
FOR i IN 1 .. v_err_count
LOOP
DBMS_OUTPUT.put_line (
'Error: '
|| i
|| ' Array Index: '
|| SQL%BULK_EXCEPTIONS (i).ERROR_INDEX
|| ' Message: '
|| SQLERRM (SQL%BULK_EXCEPTIONS (i).ERROR_CODE));
END LOOP;
END;
BEGIN
FORALL i IN 1..l_rec.COUNT SAVE EXCEPTIONS
UPDATE iflow_rules --- updating openup system iflow cost details
SET ora_org_id = 5063,
ora_inv_item_id = l_rec(i).l_r12_inv_id
WHERE iflow_id = l_rec(i).l_r12_iflow
AND ora_inv_item_id = l_rec(i).l_r12_inv_id;
EXCEPTION
WHEN user_excep
THEN
v_err_count := SQL%BULK_EXCEPTIONS.COUNT;
FOR i IN 1 .. v_err_count
LOOP
DBMS_OUTPUT.put_line (
'Error: '
|| i
|| ' Array Index: '
|| SQL%BULK_EXCEPTIONS (i).ERROR_INDEX
|| ' Message: '
|| SQLERRM (SQL%BULK_EXCEPTIONS (i).ERROR_CODE));
END LOOP;
END;
EXIT WHEN tk_iflow_cur%NOTFOUND;
END LOOP; --End Cursor Loop
COMMIT;
CLOSE tk_iflow_cur;
END;
we are trying to use PL/JSON package for our programs but it looks like that json.to_clob method has a bug. If you try this code:
-- json to clob bug in pl/json package
-- if json value is single character then that json value will not be shown in clob
declare
l_Data_json_clob clob;
l_Data_json json;
begin
l_Data_json := json();
dbms_lob.createtemporary(l_Data_json_clob, true); -- create CLOB where result will be stored
l_Data_json.put('RESULT', '1');--put one character in json value
l_Data_json.to_clob(l_Data_json_clob);--put json result to clob
dbms_output.put_line(dbms_lob.substr( l_Data_json_clob, 255, 1));--not correct json - json value is empty
l_Data_json.put('RESULT', '22');--put two characters in json value
l_Data_json.to_clob(l_Data_json_clob);--put json result to clob
dbms_output.put_line(dbms_lob.substr( l_Data_json_clob, 255, 1));--correct json - more then one character
l_Data_json.put('RESULT', 1);--put one number in json value
l_Data_json.to_clob(l_Data_json_clob);--put json result to clob
dbms_output.put_line(dbms_lob.substr( l_Data_json_clob, 255, 1));--again correct json but with number instead of string
end;
You will see that for some reason first json value is empty. We noticed that it is happening only if json value is single character. Is there any workaround for this behavior?
Thank you
After some debugging I found that the bug is in JSON_PRINTER.ppString. The correct code should be:
procedure ppString(elem json_value, buf in out nocopy clob, buf_str in out nocopy varchar2) is
offset number := 1;
v_str varchar(5000 char);
amount number := 5000; /*chunk size for use in escapeString. Maximum escaped unicode string size for chunk may be 6 one-byte chars * 5000 chunk size in multi-byte chars = 30000 1-byte chars. Maximum value may be 32767 1-byte chars */
begin
add_to_clob(buf, buf_str, case when elem.num = 1 then '"' else '/**/' end);
if(elem.extended_str is not null) then /*clob implementation*/
while(offset <= dbms_lob.getlength(elem.extended_str)) loop
dbms_lob.read(elem.extended_str, amount, offset, v_str);
if(elem.num = 1) then
add_to_clob(buf, buf_str, escapeString(v_str));
else
add_to_clob(buf, buf_str, v_str);
end if;
offset := offset + amount;
end loop;
else
if(elem.num = 1) then
while(offset**<=**length(elem.str)) loop
v_str:=substr(elem.str, offset, amount);
add_to_clob(buf, buf_str, escapeString(v_str));
offset := offset + amount;
end loop;
else
add_to_clob(buf, buf_str, elem.str);
end if;
end if;
add_to_clob(buf, buf_str, case when elem.num = 1 then '"' else '/**/' end || newline_char);
end;