Thursday, December 10, 2009

SQL order by Sort Direction as Parameter

We can specify the order by direction as parameter.

See the Following Stored procedure :

Create procedure testOrderBy
@sd int
as
begin
select * from [_LoanRequest] order by
CASE WHEN @sd = 1 THEN lenderid END DESC,
CASE WHEN @sd = 0 then lenderid END
end

To execute the stored procedue: ,

exec testorderby 1

where 1 is for the order desc
and 0 is for the order asc

hope it work for everyone who are searching to set the sort direction as parameter.

No comments:

Post a Comment