General information
Forum:
Microsoft SQL Server
Title:
Dynamic table names in Order By statement?
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
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only