Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi,I'm doing a stored procedure which will sort the resultset with a input column name. It will look like this -select * from MY_TABLE order by @ColNameIs there a way to make it without using dynamic SQL?Thanks,George
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2003-08-12 : 12:47:32
Here is an example:
DECLARE @SortOrder tinyintSET @SortOrder = 2SELECT CompanyName, ContactName, ContactTitleFROM CustomersORDER BY CASE WHEN @SortOrder = 1 THEN CompanyName WHEN @SortOrder = 2 THEN ContactName ELSE ContactTitle END