SELECT @variable = text FROM OPENXML(@hdoc, '/', 1) WHERE text IS NOT NULL AND parentid=0
Main Topics
Browse All TopicsHi everybody!
I am new to stored procedures/functions and I have a simple question:
How can I assign the result of a SELECT query to a variable?
The query is like:
SELECT text FROM OPENXML(@hdoc, '/', 1) WHERE text IS NOT NULL AND parentid=0
Thank you!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
in case '(SELECT text FROM OPENXML(@hdoc, '/', 1) WHERE text IS NOT NULL AND parentid=0) ' statement is returning more that one values , it will throw an error with the SET statement, because it works as a SubQuery. meanwhile , in case of the second method, it wont throw an error, instead it stors the last amongg those values.
aneeshattingal, thank you for the clarification.
My query is supposed to aways return only one value and that's why
I wanted to assign it to a variable. This should be (kind of) enforced by the
WHERE clause.
I just tried to run it without the WHERE clause and I got the following error:
Msg 512, Level 16, State 1, Procedure spMyParseXML, Line 20
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
this is the query without the WHERE clause:
-- the SELECT query returns 2 fields
SELECT @rslt =( SELECT text FROM OPENXML(@hdoc, '/') )
Correct me if I'm wrong but on MS SQL Server 2005
it looks like it doesn't make any difference whether I use SELECT or SET.
I just came across this article:
http://ryanfarley.com/blog
May be I should start new thread about it.
Thank you once again.
Business Accounts
Answer for Membership
by: mankowitzPosted on 2007-07-20 at 04:28:26ID: 19530077
set @tmp = (SELECT text FROM OPENXML(@hdoc, '/', 1) WHERE text IS NOT NULL AND parentid=0)