Solved

Reading values into a variable without using a loop/cursor

Posted on 2011-09-26
8
209 Views
Last Modified: 2012-08-13
Hi Experts,

many months ago I thought I remember seeing sql syntax that would allow me to create a variable and read/assign certain values to it without having to resort to a loop.

Here is my first try:

declare @result as varchar(8000)
select distinct @result = fName + ', ' from dbo.Person order by fName
print @result

I am hoping the @result variable would show me something like this:
Abby, John, Kenneth, etc....

thanks

P.S. Right now this is against SQL Server 2000.


0
Comment
Question by:yechan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36600115
try this:

declare @result as varchar(8000)

select distinct @result = isnull(@result + ', ', '') + fName  from dbo.Person order by fName

print @result
0
 
LVL 1

Author Comment

by:yechan
ID: 36600188
Hi Emoreau,

for one reason or another, the @result variable only holds the very last value (i.e. Zach).

0
 
LVL 1

Author Comment

by:yechan
ID: 36600191
I *think* I remember seeing something like this where this is possible but I am not 100% sure.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36600207
is the field possible char (and not varchar)?
0
 
LVL 1

Author Comment

by:yechan
ID: 36600296
angel:

the field itself is varchar(50).
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 125 total points
ID: 36600333
remove the distinct clause
0
 
LVL 1

Author Comment

by:yechan
ID: 36600353
@emoreau:

removing the distinct seemed to do the trick.  Can you please explain why the distinct keyword would cause only the last value to show?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36600396
I am not exactly sure of the effect of the Distinct clause here
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

628 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