SQL Server XQuery Help 'value()' requires a singleton

I am trying to write an xquery passing dynamic values .Here is my code
Here if I pass static value [1] or [2] I get the value in the tag <Method>.What I am trying to do is to loop through it to print all the values in <method> tag.For that if I pass the value @cnt,i get this error

Msg 2389, Level 16, State 1, Line 13
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
How do you pass value dynamically as in the code
DECLARE @myxml XML
Select @myxml = '<Root xmlns="http"><Name>Test</Name><Methods><Method>Air</Method><Method>Ground</Method></Methods></Root>'
DECLARE     
	@cnt INT,     
	@totCnt INT   , 
	@Value VARCHAR(10)	
SELECT     @cnt = 1    
SET @totCnt = (Select @myxml.value('declare namespace x="http";count(/x:Root/x:Methods/x:Method)', 'INT'))

WHILE @cnt <= @totCnt 
BEGIN    

Set @Value =(SELECT @myxml.value('declare namespace x="http";(/x:Root/x:Methods/x:Method)[@cnt]', 'nvarchar(100)') )
Print @Value
		SELECT @cnt = @cnt + 1
END

Open in new window

LVL 15
spprivateAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Christopher GordonSenior Developer AnalystCommented:
Just a quick observation, It *looks* like @cnt is wrapped in single quotes and won't be interpreted as the value of the variable but rather the variable name '@cnt' itself.  I'll verify in a sec.
0
spprivateAuthor Commented:
thanks.fyi i tried using [position()=sql:variable("@cnt")] as well
0
Christopher GordonSenior Developer AnalystCommented:
Does this help?

Select @myxml = '<Root xmlns="http"><Name>Test</Name><Methods><Method>Air</Method><Method>Sea</Method><Method>Ground</Method></Methods></Root>'
       
SELECT
       Method = T.Item.query('.').value('.', 'varchar(100)')
       
FROM   @myXml.nodes('declare namespace ns="http";/ns:Root/ns:Methods/ns:Method') AS T(Item)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spprivateAuthor Commented:
Thanks ,that works..Just curious.Full points to you
how would you fetch an attribute in the same manner.Say something like
<Method name="transport"> Air </Method>
I want to select "transport"
0
spprivateAuthor Commented:
Never mind.Got that with data() .Thanks a lot.Just couldnt follow much.Appreciate if you can explain it tear down.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.