Link to home
Start Free TrialLog in
Avatar of conrad2010
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
Avatar of mmr159
mmr159

You cannot sort by variable.
Avatar of conrad2010

ASKER

how can I add the ASC or DESC value as a parameter?
ASKER CERTIFIED SOLUTION
Avatar of mmr159
mmr159

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.