RE: Order by an expression.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: RE: Order by an expression.

  1. #1
    Kenneth Guest

    RE: Order by an expression.


    > Hi.
    >
    > 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?
    >
    > Regards...
    >
    > Kenneth


    Make it a string, and EXEC() it:

    DECLARE @SQL varchar(255)

    SET @SQL = 'SELECT * FROM MyTable ORDER BY ' + @T

    EXEC(@SQL)

    --
    Colin McGuigan


    Hi Colin.

    When I try to do that, it comes out with an error: Error converting varchar
    to int.

    How should I declare the variable @t in the SP?

    Thanks for your help so far.

    Regards.

    Kenneth


  2. #2
    Colin McGuigan Guest

    Re: Order by an expression.

    Kenneth <ridder@snoopymail.com> wrote in message
    news:39c1dfa4$1@news.devx.com...
    > Hi Colin.
    >
    > When I try to do that, it comes out with an error: Error converting

    varchar
    > to int.
    >
    > How should I declare the variable @t in the SP?
    >
    > Thanks for your help so far.
    >
    > Regards.
    >
    > Kenneth


    As a varchar, preferably:

    If it's part of the stored proc declaration:

    CREATE PROCEDURE MyProcedure
    @T varchar(50)
    AS

    Or if it's just declared on it's own:

    DECLARE @T varchar(50)


    --
    Colin McGuigan




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center