12 PL/SQL Package DBMS_XMLSTORE
You can use PL/SQL package DBMS_XMLSTORE
to insert, update, or delete data from XML documents stored object-relationally. It uses a canonical XML mapping similar to the one produced by package DBMS_XMLGEN
. It converts the mapping to object-relational constructs and then inserts, updates or deletes the corresponding values in relational tables.
- Using Package DBMS_XMLSTORE
Create a context handle, perform one or more insertions, updates, or deletions, and then close the context, - Inserting an XML Document Using DBMS_XMLSTORE
You can use PL/SQL packageDBMS_XMLSTORE
to insert an XML document into a table or view. It parses the document and creates anINSERT
statement into which it binds the values. By default, it inserts values into all of the columns represented by elements in the XML document. - Updating XML Data Using DBMS_XMLSTORE
You can use PL/SQL packageDBMS_XMLSTORE
to update (modify) existing data. You specify which rows to update by calling proceduresetKeyColumn
once for each of the columns that are used collectively to identify the row. (In SQL, you would specify the rows using aWHERE
clause in anUPDATE
statement.) - Deleting XML Data Using DBMS_XMLSTORE
You can use PL/SQL packageDBMS_XMLSTORE
to delete existing data. You specify which rows to delete by calling proceduresetKeyColumn
once for each of the columns that are used collectively to identify the row. (In SQL, you would specify the rows using aWHERE
clause in anUPDATE
statement.)
Parent topic: XMLType APIs
12.1 Using Package DBMS_XMLSTORE
Create a context handle, perform one or more insertions, updates, or deletions, and then close the context,
To use PL/SQL package DBMS_XMLSTORE
, follow these steps:
Parent topic: PL/SQL Package DBMS_XMLSTORE
12.2 Inserting an XML Document Using DBMS_XMLSTORE
You can use PL/SQL package DBMS_XMLSTORE
to insert an XML document into a table or view. It parses the document and creates an INSERT
statement into which it binds the values. By default, it inserts values into all of the columns represented by elements in the XML document.
Example 12-1 uses DBM_XMLSTORE
to insert the information for two new employees into the employees
table. The information to insert is provided as XML data.
Example 12-1 Inserting Data with Specified Columns
SELECT employee_id AS EMP_ID, salary, hire_date, job_id, email, last_name
FROM employees WHERE department_id = 30;
EMP_ID SALARY HIRE_DATE JOB_ID EMAIL LAST_NAME
------ ---------- --------- ---------- ---------- ----------
114 11000 07-DEC-94 PU_MAN DRAPHEAL Raphaely
115 3100 18-MAY-95 PU_CLERK AKHOO Khoo
116 2900 24-DEC-97 PU_CLERK SBAIDA Baida
117 2800 24-JUL-97 PU_CLERK STOBIAS Tobias
118 2600 15-NOV-98 PU_CLERK GHIMURO Himuro
119 2500 10-AUG-99 PU_CLERK KCOLMENA Colmenares
6 rows selected.
DECLARE
insCtx DBMS_XMLSTORE.ctxType;
rows NUMBER;
xmlDoc CLOB :=
'<ROWSET>
<ROW num="1">
<EMPLOYEE_ID>920</EMPLOYEE_ID>
<SALARY>1800</SALARY>
<DEPARTMENT_ID>30</DEPARTMENT_ID>
<HIRE_DATE>17-DEC-2002</HIRE_DATE>
<LAST_NAME>Strauss</LAST_NAME>
<EMAIL>JSTRAUSS</EMAIL>
<JOB_ID>ST_CLERK</JOB_ID>
</ROW>
<ROW>
<EMPLOYEE_ID>921</EMPLOYEE_ID>
<SALARY>2000</SALARY>
<DEPARTMENT_ID>30</DEPARTMENT_ID>
<HIRE_DATE>31-DEC-2004</HIRE_DATE>
<LAST_NAME>Jones</LAST_NAME>
<EMAIL>EJONES</EMAIL>
<JOB_ID>ST_CLERK</JOB_ID>
</ROW>
</ROWSET>';
BEGIN
insCtx := DBMS_XMLSTORE.newContext('HR.EMPLOYEES'); -- Get saved context
DBMS_XMLSTORE.clearUpdateColumnList(insCtx); -- Clear the update settings
-- Set the columns to be updated as a list of values
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'EMPLOYEE_ID');
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'SALARY');
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'HIRE_DATE');
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'DEPARTMENT_ID');
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'JOB_ID');
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'EMAIL');
DBMS_XMLSTORE.setUpdateColumn(insCtx, 'LAST_NAME');
-- Insert the doc.
rows := DBMS_XMLSTORE.insertXML(insCtx, xmlDoc);
DBMS_OUTPUT.put_line(rows || ' rows inserted.');
-- Close the context
DBMS_XMLSTORE.closeContext(insCtx);
END;
/
2 rows inserted.
PL/SQL procedure successfully completed.
SELECT employee_id AS EMP_ID, salary, hire_date, job_id, email, last_name
FROM employees WHERE department_id = 30;
EMP_ID SALARY HIRE_DATE JOB_ID EMAIL LAST_NAME
------ ---------- --------- ---------- ---------- ----------
114 11000 07-DEC-94 PU_MAN DRAPHEAL Raphaely
115 3100 18-MAY-95 PU_CLERK AKHOO Khoo
116 2900 24-DEC-97 PU_CLERK SBAIDA Baida
117 2800 24-JUL-97 PU_CLERK STOBIAS Tobias
118 2600 15-NOV-98 PU_CLERK GHIMURO Himuro
119 2500 10-AUG-99 PU_CLERK KCOLMENA Colmenares
920 1800 17-DEC-02 ST_CLERK STRAUSS Strauss
921 2000 31-DEC-04 ST_CLERK EJONES Jones
8 rows selected.
Parent topic: PL/SQL Package DBMS_XMLSTORE
12.3 Updating XML Data Using DBMS_XMLSTORE
You can use PL/SQL package DBMS_XMLSTORE
to update (modify) existing data. You specify which rows to update by calling procedure setKeyColumn
once for each of the columns that are used collectively to identify the row. (In SQL, you would specify the rows using a WHERE
clause in an UPDATE
statement.)
You can think of this set of columns as acting like a set of key columns: together, they specify a unique row to be updated. However, the columns that you use (with setKeyColumn
) need not be keys of the table — as long as they uniquely specify a row, they can be used with calls to setKeyColumn
.
Example 12-2 uses DBM_XMLSTORE
to update information. Assuming that the first name for employee number 188 is incorrectly recorded as Kelly, this example corrects that first name to Pat. Since column employee_id
is a primary key for table employees
, a single call to setKeyColumn
specifying column employee_id
is sufficient to identify a unique row for updating.
The following UPDATE
statement is equivalent to the use of DBM_XMLSTORE
in Example 12-2:
UPDATE hr.employees SET first_name = 'Pat' WHERE employee_id = 188;
Example 12-2 Updating Data with Key Columns
SELECT employee_id, first_name FROM employees WHERE employee_id = 188;
EMPLOYEE_ID FIRST_NAME
----------- ----------
188 Kelly
1 row selected.
DECLARE
updCtx DBMS_XMLSTORE.ctxType;
rows NUMBER;
xmlDoc CLOB :=
'<ROWSET>
<ROW>
<EMPLOYEE_ID>188</EMPLOYEE_ID>
<FIRST_NAME>Pat</FIRST_NAME>
</ROW>
</ROWSET>';
BEGIN
updCtx := DBMS_XMLSTORE.newContext('HR.EMPLOYEES'); -- get the context
DBMS_XMLSTORE.clearUpdateColumnList(updCtx); -- clear update settings
-- Specify that column employee_id is a "key" to identify the row to update.
DBMS_XMLSTORE.setKeyColumn(updCtx, 'EMPLOYEE_ID');
rows := DBMS_XMLSTORE.updateXML(updCtx, xmlDoc); -- update the table
DBMS_XMLSTORE.closeContext(updCtx); -- close the context
END;
/
SELECT employee_id, first_name FROM employees WHERE employee_id = 188;
EMPLOYEE_ID FIRST_NAME
----------- ----------
188 Pat
1 row selected.
Parent topic: PL/SQL Package DBMS_XMLSTORE
12.4 Deleting XML Data Using DBMS_XMLSTORE
You can use PL/SQL package DBMS_XMLSTORE
to delete existing data. You specify which rows to delete by calling procedure setKeyColumn
once for each of the columns that are used collectively to identify the row. (In SQL, you would specify the rows using a WHERE
clause in an UPDATE
statement.)
Example 12-3 DBMS_XMLSTORE.DELETEXML Example
SELECT employee_id FROM employees WHERE employee_id = 188;
EMPLOYEE_ID
-----------
188
1 row selected.
DECLARE
delCtx DBMS_XMLSTORE.ctxType;
rows NUMBER;
xmlDoc CLOB :=
'<ROWSET>
<ROW>
<EMPLOYEE_ID>188</EMPLOYEE_ID>
<DEPARTMENT_ID>50</DEPARTMENT_ID>
</ROW>
</ROWSET>';
BEGIN
delCtx := DBMS_XMLSTORE.newContext('HR.EMPLOYEES');
DBMS_XMLSTORE.setKeyColumn(delCtx, 'EMPLOYEE_ID');
rows := DBMS_XMLSTORE.deleteXML(delCtx, xmlDoc);
DBMS_XMLSTORE.closeContext(delCtx);
END;
/
SELECT employee_id FROM employees WHERE employee_id = 188;
no rows selected.
Parent topic: PL/SQL Package DBMS_XMLSTORE