Solved

build string from select statement

Posted on 2008-10-18
5
852 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

15 Experts available now in Live!

Get 1:1 Help Now