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
Select @myxml = '<Root xmlns="http"><Name>Test</Name><Methods><Method>Air</Method><Method>Ground</Method></Methods></Root>'
	@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 

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

Open in new window

LVL 15
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.
spprivateAuthor Commented:
thanks.fyi i tried using [position()=sql:variable("@cnt")] as well
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>'
       Method = T.Item.query('.').value('.', 'varchar(100)')
FROM   @myXml.nodes('declare namespace ns="http";/ns:Root/ns:Methods/ns:Method') AS T(Item)

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"
spprivateAuthor Commented:
Never mind.Got that with data() .Thanks a lot.Just couldnt follow much.Appreciate if you can explain it tear down.
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

From novice to tech pro — start learning today.