Here the problem-
I am building a site that will use stored procedures to return record sets from the server to the web using ASP. The problem is that I want to allow my users to sort those reports based off the field headers. Now, if I was using a view instead of a stored procedure I could simply pass whatever field the user selected back to the SQL statement as a variable, and when processed the server wouldn't know the difference. i.e.
SQL = "Select * from employees where id = '" & strid & "' order by " & field_var & "" & dir_var & ""
But now I am trying to do it using a stored procedures. So that I can simply call a exec command on the stored procedure and pass the field to sort by and the direction as a parameter in the exec command i.e.
Exec sp_employees field, dir
To do that I have to have this in my procedure.
Create procedure employees
@field varchar(10)
@dir varchar(4)
@id varchar(4)
as
Select * from employees where id = @id
order by @field @dir
What happens when I try to save this procedure on the server is the following error.
Error 1008: The SELECT item identified by the ORDER BY number1 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.
Now I would understand it if I got a error after I tried to execute the procedure becuase the format of the value I put into the variable was wrong, but it won't even let me create the procedure with a variable in the order by clause.
Any ideas would be greatly appreciated. I am completely lost on this one.
I am building a site that will use stored procedures to return record sets from the server to the web using ASP. The problem is that I want to allow my users to sort those reports based off the field headers. Now, if I was using a view instead of a stored procedure I could simply pass whatever field the user selected back to the SQL statement as a variable, and when processed the server wouldn't know the difference. i.e.
SQL = "Select * from employees where id = '" & strid & "' order by " & field_var & "" & dir_var & ""
But now I am trying to do it using a stored procedures. So that I can simply call a exec command on the stored procedure and pass the field to sort by and the direction as a parameter in the exec command i.e.
Exec sp_employees field, dir
To do that I have to have this in my procedure.
Create procedure employees
@field varchar(10)
@dir varchar(4)
@id varchar(4)
as
Select * from employees where id = @id
order by @field @dir
What happens when I try to save this procedure on the server is the following error.
Error 1008: The SELECT item identified by the ORDER BY number1 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.
Now I would understand it if I got a error after I tried to execute the procedure becuase the format of the value I put into the variable was wrong, but it won't even let me create the procedure with a variable in the order by clause.
Any ideas would be greatly appreciated. I am completely lost on this one.