Using Variables in Order By Clause

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kamran76
    New Member
    • Jan 2007
    • 1

    Using Variables in Order By Clause

    I want to pass a user value through a variable in the order by clause and also wanna pass value of ASC/DESC through variable from the web application. Please Help
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    The only way I know is to concatenate all necessary variables in SQL statement first and then execute it.
    The problem is that variables have length limitations.
    Example:

    [PHP]Declare @OrderBY varchar(20),
    @Order varchar(10), @SQL varchar(8000)

    Set @OrderBY = 1
    Set @Order = 'desc'

    select @SQL = 'Select * from sysobjects Order by ' + @OrderBY + ' ' + @Order

    execute(@SQL)[/PHP]

    Comment

    Working...