Tuesday, September 7, 2010

How to Set Dynamically order by Clause in sql Query ?

DECLARE @iSort int
SET @iSort = 3
SELECT * FROM tableName
order by
case when @iSort = 1 then Column1 end asc,
case when @iSort = 2 then Column2 end desc,
case when @iSort = 3 then Column3 end asc,
case when @iSort = 4 then Column4 end desc

No comments:

Post a Comment