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 <email@example.com> 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)
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL