Click here to monitor SSC
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
jacob sebastian
jacob sebastian
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 2523
Yes, it can work on an XML column too. Here is an example


DECLARE @t TABLE (id int, data XML)
INSERT INTO @t(id, data) SELECT 1, '




'

INSERT INTO @t(id, data) SELECT 2, '




'

UPDATE @t
SET data.modify('
delete (Employees/Employee[@Team="SQL Server"]/@city)
')
SELECT * FROM @t



.
ynevet
ynevet
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
Hi, if your answer is intended for me then I think I didn't clear my self enough.

I'm asking if it possible to access XML file from XQuery instead of querying an XML variable.

Yair
jacob sebastian
jacob sebastian
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 2523
You can load the content of an XML file to an XML variable using OPENROWSET(BULK..). Once the content of the XML file is loaded into the XML variable, you can query it.

.
ynevet
ynevet
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
And it's possible to modify the file it's self using XQuery, that is, updating, deleting, inserting....
jacob sebastian
jacob sebastian
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 2523
Well, I would suggest using some other tools such as SSIS if you want to modify the file.

.
Sinh Dinh
Sinh Dinh
SSC-Enthusiastic
SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)

Group: General Forum Members
Points: 191 Visits: 566
I have multiple XML fields with the same schema. Is it possible to "concatenate" them in a select statement to generate a single XML field?

Table Schema:

CREATE TABLE Table1 (
ID INT
,XML1 XML
,XML2 XML )



Example:
XML Field 1:

<Response>
<Value>Idaho</Value>
<Result>ID</Result>
<Return>50</Return>
</Response>


XML Field 2:

<Response>
<Value>Texas</Value>
<Result>TX</Result>
<Return>50</Return>
</Response>


Resulting XML Field:

<Response>
<Value>Texas</Value>
<Result>TX</Result>
<Return>50</Return>
</Response>
<Response>
<Value>Texas</Value>
<Result>TX</Result>
<Return>50</Return>
</Response>


jacob sebastian
jacob sebastian
SSC-Addicted
SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)SSC-Addicted (472 reputation)

Group: General Forum Members
Points: 472 Visits: 2523
Is it what you are looking for?

SELECT (
SELECT XML1, XML2
FROM table1
FOR XML PATH(''), TYPE
).query('/*/Response')
/*
<Response>
<Value>Idaho</Value>
<Result>ID</Result>
<Return>50</Return>
</Response>
<Response>
<Value>Texas</Value>
<Result>TX</Result>
<Return>50</Return>
</Response>
*/



.
Sinh Dinh
Sinh Dinh
SSC-Enthusiastic
SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)SSC-Enthusiastic (191 reputation)

Group: General Forum Members
Points: 191 Visits: 566
Sorry for the late reply. That worked for me. Thanks.
mypoints01
mypoints01
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 99
DECLARE @XML XML
DECLARE @ElementName VARCHAR(20)
SET @ElementName = 'city'
SELECT @XML = '
<employees>
<employee>
<name>Jacob</name>
<city>NY</city>
<team>SQL Server</team>
</employee>
<employee>
<name>Steve</name>
<city>NY</city>
<team>ASP.NET</team>
</employee>
</employees>'

--SET @XML.modify('delete (/employees/employee/city)')
--SELECT @XML


But I need to delete all elements with the name equal @ElementName = 'city' and
the name of the element should be variable.
Is it possible?

Thanks, M
mypoints01
mypoints01
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 99
It seems to me that I found the solution:

DECLARE @XML XML
DECLARE @ElementName VARCHAR(20)
SET @ElementName = 'city'
SELECT @XML = '
<employees>
<employee>
<name>Jacob</name>
<city>NY</city>
<team>SQL Server</team>
</employee>
<employee>
<name>Steve</name>
<city>NY</city>
<team>ASP.NET</team>
</employee>
</employees>'

--SET @XML.modify('delete (/employees/employee/*[local-name() = "city"])')
SET @XML.modify('delete (/employees/employee/*[local-name() = sql:variable("@ElementName")])')
SELECT @XML
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