XML DML
The XQuery specification does not provide a way for you to modify data, as you would have with the INSERT, UPDATE, and DELETE
keywords in T-SQL. The W3C has a working draft, but it might be a few
years before it becomes a standard, so Microsoft has created its own XML
data manipulation language, XML DML, which is included in its XQuery
implementation. The Microsoft version conforms to the W3C working draft,
but by no means do we know the final standard’s look and feel.
XML DML gives you three ways to manipulate the XML data of a column via the xml.modify() function:
xml.modify(insert) Allows you to insert a node or sequence of nodes into the XML data type instance you are working with
xml.modify(delete) Allows you to delete zero or more nodes that are the result of the output sequence of the XQuery expression you specify
xml.modify(replace) Modifies the value of a single node
xml.modify(insert)
The xml.modify(insert) function allows you to insert a node or sequence of nodes into the XML data type instance you are working with. You use the xml.modify()UPDATE statement and, if necessary, a T-SQL or XQuery where clause (or both). You can also specify the keyword first or last beside an ordinal position. function in conjunction with a T-SQL
--insert an XML node
Update tblSpeakers
Set Speaker_XML.modify(
'insert
<class name="Ranking and Windowing Functions in SQL Server 2005"/>
into /classes[1]'
)
Where Speaker_ID=1
xml.modify(delete)
The xml.modify(delete)
function deletes zero or more nodes based on the criteria you specify.
The following example deletes a specific node based on its ordinal
position; you can also combine this with a T-SQL or XQuery WHERE clause.
Update tblSpeakers
Set Speaker_XML.modify('delete /classes/class[3]')
Where Speaker_ID=1
xml.modify(replace)
--modify a value
Update tblSpeakers
Set Speaker_XML.modify('
replace value of
/classes/class[3]
to "Protecting against SQL Injection Attacks-Hackers must die"')
Where Speaker_ID=1
Converting a Column to XML
If you have existing text or varchar() columns in SQL Server 2000 that contain XML data, you’re probably excited about using the XML data type and XQuery. Because you can use the T-SQL CONVERT and CAST functions to convert those columns into full-fledged XML data, you can write a simple upgrade script.
First we need a table. For this example, we'll create a simple table:
Create table tblXMLUpgradeTest
(Field_ID int Primary Key Identity,
Field_XMLData nvarchar(4000))
Next we insert some data, but remember that it is stored as text, not XML.
--insert some data as txt but is XML
--remember no cool XQuery or anything allowed!
DECLARE @xmlData AS varchar(8000)
SET @xmlData = (Select Customer.CustomerID,
OrderDetail.SalesOrderID,OrderDetail.OrderDate
From Sales.Customer Customer
inner join Sales.SalesOrderHeader OrderDetail
on OrderDetail.customerid=Customer.customerid
where Customer.CustomerID<5
Order by Customer.CustomerID
For XML AUTO, Root ('Orders'))
--first insert into the table
Insert Into tblXMLUpgradeTest (Field_XMLData) Values (@xmlData)
You can now convert your XML data from text to an XML column in a series of six steps:
1. | In the table containing the text field holding the XML data, add a varchar(8000) or text field, depending on what your original column is.
|
2. | Append the data in your current production text-based column into the new XML-based column.
|
3. | Drop your text-based column.
|
4. | Add the original column name back in, but with an XML data type.
|
5. | Insert the data from the temporary column, using the CONVERT statement to convert the data to an XML data type.
|
6. | Drop the temporary column.
A code example of all six steps is shown here.
--first alter the table ALTER TABLE tblXMLUpgradeTest ADD Field_XMLData_Temp varchar(8000) go --second update the Field_XMLData_Temp column Update tblXMLUpgradeTest set Field_XMLData_Temp=Field_XMLData go --third drop the original text column (Field_XMLData) ALTER TABLE tblXMLUpgradeTest Drop Column Field_XMLData go --fourth add the same column(Field_XMLData) but as XML ALTER TABLE tblXMLUpgradeTest ADD Field_XMLData XML go --fifth insert the XML into Field_XMLData and convert to XML data type Update tblXMLUpgradeTest set Field_XMLData=Convert(XML, Field_XMLData_Temp) go --sixth drop the Field_XMLData_Temp column since it has text, not XML ALTER TABLE tblXMLUpgradeTest Drop Column Field_XMLData_Temp go |