Link to home
Start Free TrialLog in
Avatar of PsychoDazey
PsychoDazey

asked on

build string from select statement

I am trying to build a select statement that I will pass to a tored proc as xml.  basically, I want to wrap the ids that are returned in xml tags, like this:
<id>123</id><id>234</id>
...the query statement I am trying to run looks like:
select distinct @myxml = '<id>' + convert(varchar(25), id) + '</id>'
from tbl_answers
where coalesce(IsCompleted, 0) = 0

This only returns the first value it hits (i.e. - <id>123</id>).  I also tried inserting the results into a table variable and building the string by selecting one row at a time, that didnt work either, it returns a null.

Any thoughts?
Avatar of Binuth
Binuth
Flag of India image

try this
declare @myxml as varchar(max)
set @myxml = ''
select @myxml = @myxml + '<id>' + convert(varchar(25), id) + '</id>'
from tbl_answers
where coalesce(IsCompleted, 0) = 0

Open in new window

Avatar of PsychoDazey
PsychoDazey

ASKER

That works until I put the distinct clause in.  For some reason as soon as I do that it only returns the first record....any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Binuth
Binuth
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect, thanks!
sorry, forgot to accept!