Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can I use a variable ORDER BY clause?
Message
From
24/04/2007 09:01:34
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Can I use a variable ORDER BY clause?
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01219282
Message ID:
01219282
Views:
53
Can I use a variable ORDER BY clause? It is rejected by the syntax parser as it wants the name of a column. Is there a way other than creating a different query for each?
Maybe using an ugly CASE WHEN in ORDER BY?

This is a very simplied stored proc to illustrate:
CREATE PROCEDURE dbo.sp_getWhatever
(
   @SortOrder char(10)
)
AS

SET NOCOUNT ON

SELECT o.Org_Name, c.CtryNm  -- extra sortable columns here
FROM Orgs AS o
INNER JOIN Countries AS c ON o.ctry_code = c.Ctry_Cd
ORDER BY @SortOrder
Still learning T-SQL :)


Alex Feldstein, MCP, Microsoft MVP
VFP Tips: English - Spanish
Website - Blog - Photo Gallery


"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry
Next
Reply
Map
View

Click here to load this message in the networking platform