Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

get all values separated by commas

By Luis Costa,

Just for fun, i have to get all values from a table, into an 'array' like, all values separated with commas.

Some guys told me to use a cursor or an while ... loop.

The mission is only a concatenate, why i have loop it?

What you have to do is:

  1. First you have to declare a string.
  2. Then you can concatenate all the rows into the string,
  3. and finnaly you have to cut the tips.

Simple?

SELECT is like a loop that you can do some stuff on each row.

Some one told that if you do SELECT @i=id FROM XPTO

you will get the last id, that is almost wrong. You get the last because you are setting, on each row, a new vaue to @i.

But if we always keep the value of @i we can get all the records in a row.

Have fun TSQLing.

Total article views: 2121 | Views in the last 30 days: 2
 
Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones