Results 1 to 7 of 7

Thread: Stored Procedure Order By Variable

  1. #1
    Lively Member
    Join Date
    Oct 03
    Posts
    96

    Stored Procedure Order By Variable

    I want to query my table with a stored procedure with a way for the user of my program to change how the data is ordered.

    So I wrote my Stored procedure out like this...

    Code:
    CREATE     PROCEDURE spVerifyComplete
    
    (
    @Order1 as column,
    @Order2  as column
    )
    
    As
     SELECT
    *
     FROM RSR
     WHERE (RSRCompleted = "NO"
    ORDER BY @Order1, @Order2
    
    GO
    But it don't work.

    Can someone tell me what I did wrong?

    Thanks,

    -Ober

  2. #2
    Unmoderated abhijit's Avatar
    Join Date
    Jun 99
    Location
    Chit Chat Forum.
    Posts
    3,117
    Its not going to work this way.

    You need to assign the whole query to a variable and use the exec statement to run it correctly.

    Cheers,
    Abhijit


    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text File

  3. #3
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 02
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    WHERE (RSRCompleted = "NO"
    ---------^

    could it be that?

  4. #4
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 01
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    As abhijit said:
    Code:
    CREATE     PROCEDURE spVerifyComplete
    
    (
    @Order1 as column,
    @Order2  as column
    )
    
    As
    
    Declare @Sql varchar(8000)
    Set @Sql = 'Select * '
    Set @Sql = @Sql + 'From RSR '
    Set @Sql = @Sql + 'Where Completed = "NO" '
    Set @Sql = @Sql + 'Order By ' + @Order1 + ', ' + @Order2
    
    Exec (@Sql)
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  5. #5
    Lively Member
    Join Date
    Oct 03
    Posts
    96
    Yup,

    I wrote that after working all day/night on Christmas day.

    By morning of the day after Christmas, I was tired and when your tired and not to mention hungry its hard to find the simple errors.

    I cannot connect to the SQL server from home so I'll have to go to work and fix it. ::sigh::

    Thanks for your help.

    -Ober

  6. #6
    Hyperactive Member
    Join Date
    Nov 99
    Location
    Leavenworth KS USA
    Posts
    482
    Column datatype?!?! Also, list the specific table columns in an SQL statement.

  7. #7
    Lively Member
    Join Date
    Jul 02
    Location
    California
    Posts
    77
    Originally posted by OberCanober
    Yup,

    -Ober
    Can this work with LIKE in a where clause. I attempted to use this with LIKE and % and I get a syntax error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •