Tuesday, November 07, 2006

SQL: Recursive Select Variable

Using this method we can convert a vertical list into horizontal list. Atleast two uses are suggested by Paul Nielson:
  1. Denormalizing a list
  2. Dynamic Cross tabs query

In following example we will see how can we denormalize a list i.e. converting a vertical list into horizontal list.

use NorthWind;
DECLARE @Name nvarchar (2200)
SET @Name = ''
SELECT @Name = @Name + A.Name + '; ' FROM (Select DISTINCT CompanyName AS Name FROM Customers) AS A
PRINT @Name

No comments: