conrad2010
asked on
T-SQL @SortOrder incorrect syntax
what is the problem with this query? The Error message: "Incorrect syntax near '@SortOrder'."
declare
@SortColumn varchar(100)
, @SortOrder varchar(5)
set @SortColumn = 'Title'
set @SortOrder = 'ASC'
select
*
from
Projects
Order By
@SortColumn @SortOrder
Untitled.jpg
declare
@SortColumn varchar(100)
, @SortOrder varchar(5)
set @SortColumn = 'Title'
set @SortOrder = 'ASC'
select
*
from
Projects
Order By
@SortColumn @SortOrder
Untitled.jpg
You cannot sort by variable.
ASKER
how can I add the ASC or DESC value as a parameter?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
The second option in my previous post is called dynamic SQL. You will need that in order to do what you are trying to do. It is not recommended. Typically, a default sort is done with the query, then the front end is allowed to sort by column asc/desc.
No prob.