Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Sort By parameter?

    I'd like to sort my stored procedure by the column name I pass in...how
    would I do so?


    CREATE PROCEDURE dbo.spHelpDeskGetItemsForHistory
    (
    @SortBy varchar(11),
    @SortDirection varchar(4)
    )
    AS
    SET NOCOUNT ON

    SELECT tblHelpDeskCategories.DisplayName AS Category,
    tblHelpDeskProblems.DateTimeStamp AS DateTime,
    tblHelpDeskProblems.BriefDesc AS Description,
    tblPeople.FName, tblPeople.LName
    FROM tblHelpDeskProblems
    INNER JOIN
    tblPeople
    ON tblHelpDeskProblems.UserID = tblPeople.PersonID
    INNER JOIN
    tblHelpDeskCategories
    ON tblHelpDeskProblems.CategoryID = tblHelpDeskCategories.ID
    ORDER BY [@SortBy] @SortDirection



    TomB Guest

  2. Similar Questions and Discussions

    1. Sort by id
      Now I simple forum by Dreamweaver i use "repeat region" for show data in database . It show like this 1 2 3 4 but i want to show
    2. Sort bug
      It appears that the runtime cannot sort a datagrid column when the items in the column contain a NaN or Infinity. Is there a way to override the...
    3. Date Parameter For Saved Parameter Queries
      Hi again, I finally got to using saved parameter queries in my application (a big thank you to Bob Barrows for helping me with this). Currently...
    4. memory sort and disk sort
      I check the sysprofile table and find there are 700 times disk sort, I think it is lack of sort memory. I want to turn all the disk sort into the...
    5. Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB.
      Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp...
  3. #2

    Default Re: Sort By parameter?

    TomB wrote:
    > I'd like to sort my stored procedure by the column name I pass
    > in...how would I do so?
    > .
    One way is to use dynamic sql:
    [url]http://www.algonet.se/~sommar/dynamic_sql.html[/url]

    Another is to use CASE
    >
    > CREATE PROCEDURE dbo.spHelpDeskGetItemsForHistory
    > (
    > @SortBy varchar(11),
    > @SortDirection varchar(4)
    <snip>
    > ORDER BY
    CASE WHEN @sortby='DisplayName' AND @SortDirection
    ='ASC' THEN DisplayName ELSE 0 END ASC,
    CASE WHEN @sortby='DisplayName' AND @SortDirection
    ='DESC' THEN DisplayName ELSE 0 END DESC,
    etc.

    HTH,
    Bob Barrows


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows Guest

  4. #3

    Default Re: Sort By parameter?

    > > ORDER BY
    > CASE WHEN @sortby='DisplayName' AND @SortDirection
    > ='ASC' THEN DisplayName ELSE 0 END ASC,
    > CASE WHEN @sortby='DisplayName' AND @SortDirection
    > ='DESC' THEN DisplayName ELSE 0 END DESC,
    In addition, a trick I learned (at least when dealing with positive numeric
    columns) is to use:

    CASE @sortby
    WHEN 'IntColumn' THEN
    CASE @sortDirection
    WHEN 'ASC' THEN IntColumn
    ELSE -IntColumn END
    ...
    END


    Aaron Bertrand [MVP] Guest

  5. #4

    Default Re: Sort By parameter?

    Thanks, I thought I was missing something.


    "Bob Barrows" <reb01501@NOyahoo.SPAMcom> wrote in message
    news:um9DWJ$nDHA.1632@TK2MSFTNGP10.phx.gbl...
    > TomB wrote:
    > > I'd like to sort my stored procedure by the column name I pass
    > > in...how would I do so?
    > > .
    > One way is to use dynamic sql:
    > [url]http://www.algonet.se/~sommar/dynamic_sql.html[/url]
    >
    > Another is to use CASE
    > >
    > > CREATE PROCEDURE dbo.spHelpDeskGetItemsForHistory
    > > (
    > > @SortBy varchar(11),
    > > @SortDirection varchar(4)
    > <snip>
    > > ORDER BY
    > CASE WHEN @sortby='DisplayName' AND @SortDirection
    > ='ASC' THEN DisplayName ELSE 0 END ASC,
    > CASE WHEN @sortby='DisplayName' AND @SortDirection
    > ='DESC' THEN DisplayName ELSE 0 END DESC,
    > etc.
    >
    > HTH,
    > Bob Barrows
    >
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >

    Tom B Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139