Solved

Reading values into a variable without using a loop/cursor

Posted on 2011-09-26
8
166 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
  • 4
  • 3
8 Comments
 
LVL 69

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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36600207
is the field possible char (and not varchar)?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 1

Author Comment

by:yechan
ID: 36600296
angel:

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

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 69

Expert Comment

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

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now