Solved

Reading values into a variable without using a loop/cursor

Posted on 2011-09-26
8
176 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

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…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

24 Experts available now in Live!

Get 1:1 Help Now