Tuesday, October 30, 2018

DBMS_XMLSTORE: Insert, Update, Delete XML data

DBMS_XMLSTORE: Insert, Update, Delete XML data

Overview:
DBMS_XMLSTORE API / Package enables DML operations to be performed on relational tables using XML data.
Steps to use DBMS_XMLSTORE:
1. Create a context handle by calling function DBMS_XMLSTORE.newContext and supplying it with the table name to use for the DML operations. For case sensitivity, double-quote (“) the string that is passed to the function.
2. By default, XML documents are expected to use the tag to identify rows. This is the same default used by package DBMS_XMLGEN when generating XML data. You can use function setRowTag to override this behavior.
3. For inserts, to improve performance you can specify the list of columns to insert by calling procedure DBMS_XMLSTORE.setUpdateColumn for each column. The default behavior (if you do not specify the list of columns) is to insert values for each column whose corresponding element is present in the XML document.
4. For updates, use function DBMS_XMLSTORE.setKeyColumn to specify one or more (pseudo-) key columns, which are used to specify the rows to update. You do this in the WHERE clause of a SQL UPDATE statement. The columns that you specify need not be keys of the table, but together they must uniquely specify the rows to update.
For example, in table employees, column employee_id uniquely identifies rows (it is a key of the table). If the XML document that you use to update the table contains element <EMPLOYEE_ID>2176, then the rows where employee_id equals 2176 are updated.
To improve performance, you can also specify the list of update columns using DBMS_XMLSTORE.setUpdateColumn. The default behavior is to update all of the columns in the row(s) identified by setKeyColumn whose corresponding elements are present in the XML document.
5. For deletions you specify (pseudo-) key columns to identify the row(s) to delete. You do this the same way you specify rows to update — see step 3.
6. Provide a document to PL/SQL function insertXML, updateXML, or deleteXML. You can repeat this step to update several XML documents.
7. Close the context by calling function DBMS_XMLSTORE.closeContext.
Inserting with DBMS_XMLSTORE:
1
2
SELECT employee_id AS EMP_ID, salary, hire_date, job_id, email, last_name
  FROM employees WHERE department_id = 30;
Output:
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
DECLARE
   insCtx   DBMS_XMLSTORE.ctxType;
   rows     NUMBER;
   xmlDoc CLOB
         := '
         &lt;EMPLOYEE_ID&gt;920
         1800
         &lt;DEPARTMENT_ID&gt;30
         &lt;HIRE_DATE&gt;17-DEC-2002
         &lt;LAST_NAME&gt;Satya
         Venkata
         &lt;JOB_ID&gt;ST_CLERK
         &lt;EMPLOYEE_ID&gt;921
         2000
         &lt;DEPARTMENT_ID&gt;30
         &lt;HIRE_DATE&gt;31-DEC-2004
         &lt;LAST_NAME&gt;Vaddi
         Kumar
         &lt;JOB_ID&gt;ST_CLERK
     ' ;
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.
1
2
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 Venkata Satya
921 2000 31-DEC-04 ST_CLERK Kumar Vaddi
8 rows selected.
Updating with DBMS_XMLSTORE:
To update (modify) existing data using package DBMS_XMLSTORE, you must specify which rows to update. In SQL, you would do that using a WHERE clause in an UPDATE statement. With DBMS_XMLSTORE, you do it by calling procedure setKeyColumn once for each of the columns that are used collectively to identify the row.
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.
1
2
3
SELECT   employee_id, first_name
  FROM   employees
WHERE   employee_id = 188;
EMPLOYEE_ID FIRST_NAME
———– ———-
188 Kelly
1 row selected.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DECLARE
   updCtx   DBMS_XMLSTORE.ctxType;
   rows     NUMBER;
   xmlDoc CLOB
         := '
         &lt;EMPLOYEE_ID&gt;188
         &lt;FIRST_NAME&gt;Dora
     ' ;
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;
/


1
2
3
SELECT   employee_id, first_name
  FROM   employees
WHERE   employee_id = 188;

EMPLOYEE_ID FIRST_NAME
———– ———-
188 Dora
1 row selected.
Deleting with DBMS_XMLSTORE:
Deletions are treated similarly to updates: you specify the key or pseudo-key columns that identify the rows to delete.
1
2
3
SELECT   employee_id
  FROM   employees
WHERE   employee_id = 188;
EMPLOYEE_ID
———–
188
1 row selected.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DECLARE
   delCtx   DBMS_XMLSTORE.ctxType;
   rows     NUMBER;
   xmlDoc CLOB
         := '
         &lt;EMPLOYEE_ID&gt;188
         &lt;DEPARTMENT_ID&gt;50
     ' ;
BEGIN
   delCtx := DBMS_XMLSTORE.newContext ('HR.EMPLOYEES');
   DBMS_XMLSTORE.setKeyColumn (delCtx, 'EMPLOYEE_ID');
   rows := DBMS_XMLSTORE.DELETEXML (delCtx, xmlDoc);
   DBMS_XMLSTORE.closeContext (delCtx);
END;
/

1
2
3
SELECT   employee_id
  FROM   employees
WHERE   employee_id = 188;

no rows selected.

No comments:

Post a Comment

SQL Important Queries

  How to delete rows with no where clause The following example deletes  all rows  from the  Person.Person  the table in the AdventureWork...