Использование XMLTABLE
:
Установка Oracle :
CREATE TABLE tblTemplates_Load_Other_Raw(
OtherObjectsField VARCHAR2(50),
Value VARCHAR2(100)
);
CREATE PROCEDURE spInsertAppLog( LogEntry IN VARCHAR2, CodeSource IN VARCHAR2 )
AS
BEGIN
DBMS_OUTPUT.PUT_LINE( CodeSource || ' - ' || LogEntry );
END;
/
Процедура :
CREATE PROCEDURE usp_Templ_LoadXML_AddOthers(
i_XMLString IN VARCHAR2,
o_ROWCOUNT OUT NUMBER
)
AS
c_proc_name CONSTANT VARCHAR2(30) := 'usp_Templ_LoadXML_AddOthers';
p_xml XMLTYPE := NULL;
p_message VARCHAR2(4000);
BEGIN
BEGIN
p_xml := XMLType( i_XMLString );
EXCEPTION
WHEN OTHERS THEN
p_xml := NULL;
END;
IF p_xml IS NULL THEN
p_message := 'Failed to convert XMLString to xml data type.';
spInsertAppLog( LogEntry => p_message, CodeSource => c_proc_name );
RAISE_APPLICATION_ERROR( -20000, p_message );
END IF;
INSERT INTO tblTemplates_Load_Other_Raw( OtherObjectsField, Value )
SELECT fld, val
FROM XMLTABLE(
'//TPL'
PASSING p_xml
COLUMNS fld VARCHAR2(50) PATH 'fld[1]',
val VARCHAR2(100) PATH 'val[1]'
);
o_RowCount := SQL%RowCount;
EXCEPTION
WHEN OTHERS THEN
p_message := SQLERRM;
spInsertAppLog( LogEntry => p_message, CodeSource => c_proc_name );
RAISE;
END;
/
Вызов процедуры :
DECLARE
p_rows NUMBER(4,0);
BEGIN
usp_Templ_LoadXML_AddOthers(
'<root>
<TPL>
<fld>f1</fld><val>v1</val>
</TPL>
<TPL>
<fld>f2.1</fld><val>v2.1</val>
<fld>f2.2</fld><val>v2.2</val>
</TPL>
</root>',
p_rows
);
DBMS_OUTPUT.PUT_LINE( p_rows );
END;
/
выход
dbms_output:
2
Содержание таблицы :
SELECT * FROM tblTemplates_Load_Other_Raw;
OTHEROBJECTSFIELD | VALUE
:---------------- | :----
f1 | v1
f2.1 | v2.1
дБ <> скрипка здесь