Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get Data from XML Nodes for multiple rows

Author  Topic 

pricejt
Starting Member

9 Posts

Posted - 2011-11-22 : 15:55:54
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.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-23 : 00:21:49
[code]
SELECT t.u.value('@Sequence','int') as Sequence,
t.u.value('@Name','varchar(100)') as Name
FROM RulesetGroups rg
INNER JOIN Rulesets rs on
rg.RulesetGroupID = rs.RulesetGroupID
INNER JOIN Rules r on
rs.RulesetID = r.RulesetID
CROSS APPLY XMLData.Nodes('/Screen/ScreenOption')t(u)
WHERE r.RuleTypeID = 3 and t.u.value('@Name', 'varchar(255)') is not null
ORDER BY t.u.value('@Sequence','int')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pricejt
Starting Member

9 Posts

Posted - 2011-11-23 : 08:42:27
I don't understand. It says invalid object xmldata.nodes. Is that supposed to replace all of my sql or only a portion of it?
Go to Top of Page

pricejt
Starting Member

9 Posts

Posted - 2011-11-23 : 14:54:38
Sorry I got it XML is case sensitive its .nodes() not .Nodes()

Thanks so much.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-25 : 08:49:37
quote:
Originally posted by pricejt

Sorry I got it XML is case sensitive its .nodes() not .Nodes()

Thanks so much.


wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -