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


XML Workshop XVII - Writing a LOOP to process XML elements in TSQL


XML Workshop XVII - Writing a LOOP to process XML elements in TSQL

Author
Message
bhaskar.sanga
bhaskar.sanga
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 19
position()=sql:variable("@i") is not working for me, I am working on SQL 2005, basically I was trying to loop each node and insert an element..so if I use [1] or [2] its inserting in either first or second node, that's why I wanted to use while loop...in that I using the index as [position()=sql:variable("@i")] as well as [sql:variable("@i")] both are not working...please help..
jacob sebastian
jacob sebastian
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2640 Visits: 2523
Please post a small repro script that shows your problem.

.
bhaskar.sanga
bhaskar.sanga
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 19
DECLARE @index int
SET @index = 0
DECLARE @charCount as varchar(1)
DECLARE @count int
SET @charcount = CAST(@CustomerData.query('count(/customer/customer)') as varchar)
SET @Count = CAST(@charCount as int)

WHILE @index < @count
BEGIN
DECLARE @ID2 uniqueidentifier
SET @ID2 = NewID()

SET @CustomerData.modify('insert element ID {"x"} before
(/customer/customer/firstName)[sql:variable("@index")]')
SET @CustomerData.modify('replace value of (/customer/customer/ID/text())[sql:variable("@index")] with sql:variable("@ID2")')

SET @index = @index + 1
END

Instead of sql:variable("@index") if I use 1 or 2 its working..my question how to get the parameterized index
jacob sebastian
jacob sebastian
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2640 Visits: 2523
The syntax is not correct. The correct expression is [position()=sql:variable("@var")]. Here is an example:


DECLARE @x XML, @cnt INT
SELECT @x = '
<a>
<n>node 1</n>
<n>node 2</n>
<n>node 3</n>
</a>'

SELECT @cnt = 1
WHILE @cnt <= 3 BEGIN
SELECT @x.value('(/a/n[position()=sql:variable("@cnt")])[1]','VARCHAR(20)')
SELECT @cnt = @cnt + 1
END

/*
Prints

node 1
node 2
node 3
*/



.
bhaskar.sanga
bhaskar.sanga
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 19
Jocb, This is not working with SET statement, Its always inserting an element under first node.

could you please convert my example and send it back...Thanks for your help
bhaskar.sanga
bhaskar.sanga
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 19
One more thing Jocob, my main purpose of this is.. I want add an element(ID) under each node Customer Node
bhaskar.sanga
bhaskar.sanga
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 19
Never Mind Jocob...Thanks..I got it
vgermscheid
vgermscheid
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 259
Querying XML data in a set operation is relatively straight forward. Here's an excerpt from a query I have to retrieve content from a table with XML content, in this case subscriber information from a healthcare claim. First two columns in the query result are physical columns in the table, the rest come from the XML content.

Note that there's also a call to a custom assembly (NLXDecrypt) to decrypt values stored in the XML payload - portions of the content is stored encrypted to hide from prying eyes ;-).


;WITH XMLNAMESPACES ('http://Novologix.Shared.Payload.Library.Domain.Schemas' AS cm)
SELECT TOP 100 ClaimTransactionId, AddDate,
claim.content.value('SubmitterClaimIdentifier[1]','varchar(max)') SubmitterClaim,
claim.content.value('cm:NLXEntityDiagnosis[1]/DiagnosisCode[1]','varchar(max)') PrimaryDiagnosis,
claim.content.value('count(cm:NLXEntityServiceLine)','int') Lines,
admindb.dbo.NLXDecrypt(claim.content.value('cm:NLXEntityMember[1]/MemberId[1]','varchar(max)')) MemberId,
claim.content.value('cm:NLXEntityMember[1]/ClaimRole[1]','varchar(max)') ClaimRole,
admindb.dbo.NLXDecrypt(claim.content.value('cm:NLXEntityMember[1]/DateOfBirth[1]','varchar(max)')) DOB,
admindb.dbo.NLXDecrypt(claim.content.value('cm:NLXEntityMember[1]/cm:NLXEntityPerson[1]/FirstName[1]','varchar(max)')) FirstName,
admindb.dbo.NLXDecrypt(claim.content.value('cm:NLXEntityMember[1]/cm:NLXEntityPerson[1]/LastName[1]','varchar(max)')) LastName,
claim.content.value('cm:NLXEntityMember[1]/cm:NLXEntityPerson[1]/Gender[1]','varchar(max)') Gender
FROM dbo.ClaimTransaction
CROSS APPLY NLXClaim.nodes('/cm:NLXTransactionClaim/cm:NLXEntityClaim') claim(content)
ORDER BY ClaimTransactionId DESC


chintanmistry
chintanmistry
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
Hello,

How can I fetch the whole node without fetching individual fields...just like if you select it and gives you XML for single XML.
pricejt
pricejt
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 88
Ok i have this working however I still don't understand how to get all the values when my select returns more than 1 row. I assume I need a loop just not sure if there is a better way.

Here is what I have:

DECLARE @x XML;

SELECT @x =
r.XMLData
FROM RulesetGroups rg
INNER JOIN Rulesets rs on
rg.RulesetGroupID = rs.RulesetGroupID
INNER JOIN Rules r on
rs.RulesetID = r.RulesetID
WHERE r.RuleTypeID = 3 and r.XMLData.value('(/Screen/ScreenOption/@Name)[1]', 'varchar(255)') is not null;

-- select "Name"
WITH Num(i)
AS
(
SELECT 1
UNION ALL
SELECT i + 1
FROM Num
WHERE i < (SELECT @x.value('count(/Screen/ScreenOption/@Name)','varchar(255)') )
)
SELECT x.value('@Name[1]', 'varchar(20)')
FROM Num
CROSS APPLY @x.nodes('/Screen/ScreenOption[position()=sql:column("i")]') e(x);



My XML for one row. I will have hundreds of these though in different rows:

<Screen Title="">
<ScreenOption Sequence="1" Name="BTWidth" />
<ScreenOption Sequence="2" Name="BTHeight" />
</Screen>



Any help will be appreciated.
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