Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to get multiple results fetched from a query into single field??

i am trying this...
SQL
declare @uname varchar(max)
select @uname = (select uname from users)

but it is wrong...
so could anyone tell me the right way to do this??
Posted
Updated 1-Jan-12 21:58pm
v2

A select statement cannot be assigned to a scalar variable like this.

You can however create a TABLE variable, see here for examples :
http://odetocode.com/code/365.aspx[^]
 
Share this answer
 
Comments
Wendelius 2-Jan-12 4:01am    
That's a good possibility, 5.
Mehdi Gholam 2-Jan-12 4:02am    
Thanks
OriginalGriff 2-Jan-12 5:03am    
Um. You can...
DECLARE @data NVARCHAR(MAX)
SET @data =
(SELECT Id + ', ' AS 'data()'
FROM Test
FOR XML PATH(''))
PRINT @data
Sachin gulati 2-Jan-12 6:05am    
thank you... :)
You cannot place multiple items in a single variable. One way to do this is to iterate the results using a cursor. See: DECLARE CURSOR [^].

However, if possible, what ever you're doing, it would be more efficient if the operation could be done set based.
 
Share this answer
 
Comments
Mehdi Gholam 2-Jan-12 4:03am    
Yes, cursors are probably what the OP wants. 5'ed
Wendelius 2-Jan-12 4:04am    
Thank you :)
Sachin gulati 2-Jan-12 6:06am    
it worked... thanx alot... :)
Wendelius 2-Jan-12 12:35pm    
You're welcome :)
Sachin gulati 2-Jan-12 12:26pm    
can i make cursor permanent in memory?
use cursor for that if u don't know about i can write a code for cursor
 
Share this answer
 
Try:
SQL
select myColumn + ', ' as 'data()' from myTable for xml path('')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900