Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamic table names in Order By statement?
Message
From
09/01/2003 11:41:12
Alvin Lourdes
Children and Youth Services Cluster
Toronto, Ontario, Canada
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Dynamic table names in Order By statement?
Miscellaneous
Thread ID:
00739832
Message ID:
00739832
Views:
51
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
Next
Reply
Map
View

Click here to load this message in the networking platform