Solved

build string from select statement

Posted on 2008-10-18
5
849 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:PsychoDazey
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22751127
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

0
 
LVL 6

Author Comment

by:PsychoDazey
ID: 22752642
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?
0
 
LVL 14

Accepted Solution

by:
Binuth earned 500 total points
ID: 22755043

declare @myxml as varchar(max)

set @myxml = ''

select 

	@myxml = @myxml + '<id>' + convert(varchar(25), id) + '</id>'

from 

	(

		select Distinct id 

		from tbl_answers 

		where coalesce(IsCompleted, 0) = 0

	) Dist

Open in new window

0
 
LVL 6

Author Comment

by:PsychoDazey
ID: 22757962
Perfect, thanks!
0
 
LVL 6

Author Closing Comment

by:PsychoDazey
ID: 31507523
sorry, forgot to accept!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now