Results 1 to 2 of 2

Thread: Stored procedure parameter as descending

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2003
    Posts
    93

    Stored procedure parameter as descending

    Hi, Can anyone help me with the end if this stored procedure? I am trying to pass a parameter to the procedure to sort the results either ascending or descending:

    CREATE PROCEDURE cusSP_Grid

    @Sort as nvarchar(20)

    AS

    SELECT ID, BaseModel, StorisModel, CushionSet, Fabric, Type, Condition, Location, Reserved, OriginalScanBy, OriginalScanDate, EditScanBy, EditScanDate
    FROM CusCushionInv
    ORDER BY ID @Sort

    GO

    I'm interested in if this can be written like this? @Sort is a value I pass (ascending or descending) depending on the users selection. When I check my syntax in the stored procedure it tells me that I have a error: Incorrect syntax near '@Sort'

    I know that I can have the users sort by clicking on the column heading but I would like to know how to pass the sort order as a parameter. Can anyone help?

    Thanks,
    Corinne

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Uhh... no. You can't do that.... BUT, what you CAN do is build a dynamic SQL statement that the SP executes....
    Code:
    CREATE PROCEDURE cusSP_Grid
    
    @Sort as nvarchar(20)
    
    AS
    DECLARE @SQL varchar(8000)
    
    SET @SQL = 'SELECT ID, BaseModel, StorisModel, CushionSet, Fabric, Type, Condition, Location, Reserved, OriginalScanBy, OriginalScanDate, EditScanBy, EditScanDate
    FROM CusCushionInv
    ORDER BY ID ' + @Sort
    
    EXEC (@SQL) -- note the parens () are necessary....
    
    
    GO
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width