Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Parameter-driven ASC/DESC Sort?

$
0
0

Without dynamic SQL I want to achieve the following goal:

DECLARE @SortDesc bit = 1;
SELECT * FROM Sales.SalesOrderHeader
ORDER BY SalesOrderID CASE WHEN @SortDesc = 1 THEN DESC ELSE ASC END;

/*
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'CASE'.

*/

I know it can be done with dynamic SQL.  I wonder if there is a static SQL solution. Thanks.


Kalman Toth Database & OLAP Architect sqlusa.com
New Book / Kindle: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>