7.8. XML Data Manipulation LanguageXML DML extends the XQuery language to support data modification in xml data type instances. XML DML adds the insert, delete, and replace value of keywords to the XQuery language. The examples in the subsections that follow use a table named xmldmlTable. Create this table by executing the following statement: USE ProgrammingSqlServer2005 CREATE TABLE xmldmlTable ( ID int, xmlCol xml, CONSTRAINT PK_xmldmlTable PRIMARY KEY CLUSTERED (ID) ) The added XML DML keywords are used to modify xml data type instances as detailed in the following three subsections. 7.8.1. insertThe XML DML insert statement inserts one or more nodes as child nodes or siblings of a specified node in an xml data type instance. The syntax for the insert keyword follows: insert Expression1 ( {as first | as last} into | after | before Expression2 ) where:
The following example creates a record in the xmldmlTable with ID = 1 and the xmlCol xml data type column set to a simple XML document. The example then adds a child element named childElement0 as the first child of the root node using an insert XML DML statement. INSERT INTO xmldmlTable (ID, xmlCol) VALUES (1, '<root><childElement1 value="1"/></root>') SELECT xmlCol FROM xmldmlTable WHERE ID = 1 UPDATE xmldmlTable SET xmlCol.modify('insert <childElement0 value="0"/> as first into (/root)[1]') WHERE ID = 1 SELECT xmlCol FROM xmldmlTable WHERE ID = 1 Two result sets are returned, as shown in Figure 7-17. The first result set shows the xmlCol value before the XML DML insert. The second result set after the insert XML DML command shows the new childelement0 element as the first child of the root element. Figure 7-17. Results for XML DML insert example7.8.2. deleteThe XML DML delete statement deletes one or more nodes from an xml data type instance. The syntax of the delete keyword is: delete Expression where:
The following example creates a record in the xlmdmlTable with ID = 2. It assigns the elements childElement1 and childElement2 to the xlmCol column, then deletes childElement1. INSERT INTO xmldmlTable (ID, xmlCol) VALUES (2, '<root><childElement1 value="1"/> <childElement2 value="2"/></root>') SELECT xmlCol FROM xmldmlTable WHERE ID = 2 UPDATE xmldmlTable SET xmlCol.modify('delete (/root/childElement1)') WHERE ID = 2 SELECT xmlCol FROM xmldmlTable WHERE ID = 2 Two result sets are returned, as shown in Figure 7-18. Figure 7-18. Results for XML DML delete exampleThe first result set shows the xmlCol value before the XML DML delete. The second result set after the delete XML DML command shows the removal of the childelement1 element from the root element. 7.8.3. replace value ofThe XML DML replace value of statement updates the value of a node in an xml data type instance. The syntax of replace value of is: replace value of Expression1 with Expression2 where:
The following example updates the value attribute for element childElement1: INSERT INTO xmldmlTable (ID, xmlCol) VALUES (3, '<root><childElement1 value="1"/></root>') SELECT xmlCol FROM xmldmlTable WHERE ID = 3 UPDATE xmldmlTable SET xmlCol.modify('replace value of (/root/childElement1/@value)[1] with "100"') WHERE ID = 3 SELECT xmlCol FROM xmldmlTable WHERE ID = 3 Two result sets are returned, as shown in Figure 7-19. The first result set shows the xmlCol value before the XML DML replace value of. The second result set after the replace value of XML DML command shows the value attribute of the childElement1 element changed from 1 to 100. Figure 7-19. Results for XML DML replace value of example7.8.4. XML DML Limitations and RestrictionsXML DML cannot be used to insert, delete, or modify the following:
Additionally, XML DML has the following restriction:
|