tkbe

December 29, 2007

tsql :: making a list

Filed under: sql — tb @ 4:12 am

I came across the cutest little thing today, a demonstration of how to create a list in tsql by a simple select statement -- no cursors involved

SQL:
  1. CREATE TABLE country (name varchar(35))
  2. INSERT INTO country VALUES ('norway')
  3. INSERT INTO country VALUES ('sweeden')
  4. INSERT INTO country VALUES ('denmark')
  5.  
  6. declare @countries varchar(8000)
  7. SELECT @countries = isnull(@countries,'') + case when @countries IS NULL then '' else ', ' end + name FROM country

The @countries variable now has the value

SQL:
  1. norway, sweeden, denmark

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress