Connecting Tech Pros Worldwide Help | Site Map

Using Variables in Order By Clause

Newbie
 
Join Date: Jan 2007
Posts: 1
#1: Jan 6 '07
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's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#2: Jan 9 '07

re: Using Variables in Order By Clause


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]
Reply