Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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: 2176 | Views in the last 30 days: 5
 
Tags
 
Contribute