RE: Order by an expression.
> I'm trying to sort a table with an expression in a stored procedure. Like
> this :
> SELECT * FROM ___ ORDER BY @T
> But this won't work.
> Does anyone have a good idea to solve that?
Make it a string, and EXEC() it:
DECLARE @SQL varchar(255)
SET @SQL = 'SELECT * FROM MyTable ORDER BY ' + @T
When I try to do that, it comes out with an error: Error converting varchar
How should I declare the variable @t in the SP?
Thanks for your help so far.
Re: Order by an expression.
Kenneth <firstname.lastname@example.org> wrote in message
> Hi Colin.
> When I try to do that, it comes out with an error: Error converting
> to int.
> How should I declare the variable @t in the SP?
> Thanks for your help so far.
As a varchar, preferably:
If it's part of the stored proc declaration:
CREATE PROCEDURE MyProcedure
Or if it's just declared on it's own:
DECLARE @T varchar(50)
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center