tsql :: making a list
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:
-
CREATE TABLE country (name varchar(35))
-
INSERT INTO country VALUES ('norway')
-
INSERT INTO country VALUES ('sweeden')
-
INSERT INTO country VALUES ('denmark')
-
-
declare @countries varchar(8000)
-
SELECT @countries = isnull(@countries,'') + case when @countries IS NULL then '' else ', ' end + name FROM country
The @countries variable now has the value
SQL:
-
norway, sweeden, denmark