Click here to Skip to main content
15,896,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi sir,

in my front end application i take one dropdown list and datagrid. that dropdownlist purpose is sorting data. i take Ename, Esal, Dept three items to the dropdownlist.

if user select any one item from dropdownlist, that time data diplayed sorting order of selected item.

i am write stored procedure like:

SQL
create procedure sp_Sortdata
(
 @Orderbyclause varchar(50)
)
 as
   begin
        selecet * from emp order by @Orderbyclause 
    end


and i am facing this error

SQL
The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.


please correct my query sir, please give a solution

Thanks
Posted
Updated 27-Aug-10 20:52pm
v2

You can use sp_executesql for your issue, build the query based on parameter then exec with sp_executesql. Try this link Why not to use sp_executesql?[^]
 
Share this answer
 
v2
@thatraja U r right....to help a bit more I tried to write the procedure for him....

SQL
CREATE PROC sp_Sortdata
@OrderByColumnName nvarchar(15)
AS
    DECLARE @SQLStatement nvarchar(max)
    SET @SQLStatement = N'select * from emp order by '+@OrderByColumn
    EXEC sp_executesql @statement = @SQLStatement


Now execute the created stored proc by using

exec sp_SortData @OrderByColumnName='ColumnName'


Here ColumnName is the name of the column by which you want to do the sorting

Simple isn't :cool:

If you like my answer please, vote it or mark it as answer.
 
Share this answer
 
v2
SQL
DECLARE @STR VARCHAR(1000)

SET @STR = 'selecet * from emp order by '+  @Orderbyclause
EXECUTE(@STR)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900