Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

XML Workshop 24 - Modifying XML Documents Using XQuery Part 1 Expand / Collapse
Author
Message
Posted Friday, August 17, 2012 8:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, February 02, 2013 8:21 AM
Points: 283, Visits: 268
Good article. I've noticed that the "modify" function can only contain one operation per "SET". However, I want to delete multiple nodes in a piece of XML and not have to code several "SET @x.modify(...)" statements. SQL Server will not let you do "Set-based" operations multiple times on an XML column. So, maybe you can update your article with a way to delete multiple pieces of XML within the document at one time.

Use-case:
A table stores "Address" data in an Xml column called "Data". The xml is untyped but generally follows the following "schema":
<Data type="address">
<Line1 />
<Line2 />
<Line3 />
<City />
<State />
<PostalCode />
</Data>

However, the programmers have the authority to add any other Nodes they'd like in order to store other things about an address (ex. maybe the "Lat" and "Long" or the CASS Result [USPS Coding Accuracy Support System]). Any stored procedures or queries which need to update this XML data should NEVER remove any of the other nodes that they are not responsible for. In other words, do not simply overwrite the XML data, but rather just manipulate the nodes that are specific to the process at hand.

Solution:
The solution to this (because one developer from another will never know ALL the nodes that will exist in the data at any time) is to simply "delete" all the nodes that that developer knows pertains to his process, and then "insert" them back in. This way, if the nodes didn't exist to begin with, then there will be no harm. This will be much cleaner than using "replace value of" functions which rely on the nodes existing up-front (which may not be the case as time goes on and more is added to the XML).
-- Get initial data from record
DECLARE @Data xml
SET @Data = '<Data type="address">
<Line1>123 Seseame St</Line1>
<Line2 />
<Line3 />
<City>New York</City>
<Region>NY</Region>
<PostalCode>12345</PostalCode>
<PostalExtension>6789</PostalExtension>
<OtherData>My other data</OtherData>
<Lat>42</Lat>
<Long>42</Long>
</Data>'
SELECT @Data

-- Remove known nodes
SET @Data.modify('
delete (//*[local-name()=("Line1", "Line2", "Line3", "City", "Region", "PostalCode", "PostalExtension")])
')

-- Insert known nodes with updated data
DECLARE
@Line1 nvarchar(50) = '999 Somewhere Ave',
@Line2 nvarchar(50) = NULL,
@Line3 nvarchar(50) = NULL,
@City nvarchar(50) = 'Walawala',
@Region nvarchar(50) = 'WA',
@PostalCode nvarchar(50) = '99999'
DECLARE @Nodes xml
SET @Nodes = ISNULL('<Line1>' + @Line1 + '</Line1>', '')
+ ISNULL('<Line2>' + @Line2 + '</Line2>', '')
+ ISNULL('<Line3>' + @Line3 + '</Line3>', '')
+ ISNULL('<City>' + @City + '</City>', '')
+ ISNULL('<Region>' + @Region + '</Region>', '')
+ ISNULL('<PostalCode>' + @PostalCode + '</PostalCode>', '')

SET @Data.modify('
insert sql:variable("@Nodes")
as first
into (/Data)[1]
')
SELECT @Data




Post #1346583
Posted Friday, August 17, 2012 9:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, February 02, 2013 8:21 AM
Points: 283, Visits: 268
I was working on a more "set-based" approach to my above comment. However, the FLWOR statements in SQL don't seem to be working correctly.

For example, this works:
SELECT TOP 1 
ID
, Data.query('
for $n in //address/*
where (local-name($n) = ("Line1", "Line2"))
return $n
')
FROM Addresses

However, the "not equal" does NOT work. Instead, IT RETURNS ALL NODES!?
SELECT TOP 1 
ID
, Data.query('
for $n in //address/*
where (local-name($n) != ("Line1", "Line2"))
return $n
')
FROM Addresses

Basically, can anyone tell me how to make the "where" clause so that it DOES NOT return nodes with a name of "Line1" and "Line2"?



Post #1346617
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse