I'm writing some parameterised queries, and I noticed that using a parameter for the "ASC/DESC" part of the ORDER isn't allowed in SQL Server 2008. This gives the error Incorrect syntax near '@p'.
:
declare @p nvarchar
set @p = 'desc'
select * from Customer order by CustomerID @p
However using a parameter for the number of results works fine, provided you use brackets around it.
declare @p int
set @p = 5
select top (@p) * from Customer order by CustomerID
Is there any reason for this, and any workarounds?
This question suggests one workaround, but doesn't say why it's needed: DESC and ASC as a parameter in stored procedure