Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamic table names in Order By statement?
Message
 
 
To
09/01/2003 11:41:12
Alvin Lourdes
Children and Youth Services Cluster
Toronto, Ontario, Canada
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00739832
Message ID:
00739848
Views:
10
This message has been marked as the solution to the initial question of the thread.
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--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform