See
http://www.sqlmag.com/Articles/Index.cfm?ArticleID=16495.
>Hi
>I would like to dynamically set the column that I want to order by in a select statement.
>
>QUERY #1
>---------
>DECLARE @Dynamic_Col as char(50)
>SET @Dynamic_Col = "Name"
>
>SELECT DISTINCT c.Name,s.SiteName,s.ShippingAddress,FROM Orders o
>INNER JOIN Site s WITH (NOLOCK) On o.SiteID = s.SiteID
>INNER JOIN Customer c WITH (NOLOCK) On s.CustomerID = c.CustomerID
>ORDER BY @Dynamic_Col
>
>
>The Query above does not seem to work.. but the query below does..
>
>QUERY #2
>---------
>SELECT DISTINCT c.Name,s.SiteName,s.ShippingAddress,FROM Orders o
>INNER JOIN Site s WITH (NOLOCK) On o.SiteID = s.SiteID
>INNER JOIN Customer c WITH (NOLOCK) On s.CustomerID = c.CustomerID
>ORDER BY "Name"
>
>Any ideas on how I could accomplish Query #1 ?
>
>Thanks a bunch,
>
>Alvin
--sb--