SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Workshop 24 - Modifying XML Documents Using XQuery Part 1


XML Workshop 24 - Modifying XML Documents Using XQuery Part 1

Author
Message
tymberwyld
tymberwyld
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1636 Visits: 275
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





tymberwyld
tymberwyld
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1636 Visits: 275
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"?



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search