17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

If, for example, we wanted to increase the original 1.5 labor hours in our .value example, we might<br />

write something like:<br />

WITH XMLNAMESPACES (‘http://schemas.microsoft.com/sqlserver/2004/07/<br />

adventure-works/ProductModelManuInstructions’ AS PI)<br />

UPDATE Production.ProductModel<br />

SET Instructions.modify(‘replace value of (/PI:root/PI:Location/@LaborHours)[1]<br />

with 1.75’)<br />

WHERE ProductModelID = 66;<br />

Note that the URL portion of the namespace declaration must be entered on a single line. They are<br />

shown here word wrapped onto multiple lines because there is a limit to the number of characters we<br />

can show per line in print. Make sure you include the entire URL on a single line.<br />

Now if we re-run our .value command:<br />

WITH XMLNAMESPACES<br />

(‘http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuIn<br />

structions’ AS PI)<br />

SELECT ProductModelID, Instructions.value(‘(/PI:root/PI:Location/@LaborHours)[1]’,<br />

‘decimal (5,2)’) AS Location<br />

FROM Production.ProductModel<br />

WHERE ProductModelID = 66;<br />

Note that the URL portion of the namespace declaration must be entered on a single line. They are<br />

shown here word wrapped onto multiple lines because there is a limit to the number of characters we<br />

can show per line in print. Make sure you include the entire URL on a single line.<br />

We get a new value:<br />

.nodes<br />

ProductModelID Location<br />

-------------- ---------------------------------------<br />

66 1.75<br />

(1 row(s) affected)<br />

Chapter 16: A Brief XML Primer<br />

Note the way that this is essentially an UPDATE within an UPDATE. We are modifying the <strong>SQL</strong> <strong>Server</strong><br />

row, so we must use an UPDATE statement to tell <strong>SQL</strong> <strong>Server</strong> that our row of relational data (which just<br />

happens to have XML within it) is to be updated. We must also use the replace value of keyword<br />

to specify the XML portion of the update.<br />

.nodes is used to take blocks of XML and separate what would have, were it stored in a relational form,<br />

been multiple rows of data. Taking one XML document and breaking it into individual parts in this way<br />

is referred to as shredding the document.<br />

What we are doing with .nodes is essentially breaking the instances of XML data into their own table<br />

(with as many rows as there are instances of data meeting that XQuery criteria). As you might expect,<br />

this means we need to treat .nodes results as a table rather than as a column. The primary difference<br />

495

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!