Dyanamic ORDER BY

  • HI,

    I have the below SP.I want to sort the data according to @Sortby that is passed...

    Here is my Code,

    ALTER PROCEDURE GetInactiveUserdetail

    @PageIndex int,

    @NoOfRows int,

    @SortBy varchar(20),

    @UsersCount int output

    AS

    DECLARE @startRowIndex int;

    BEGIN

    SELECT

    @UsersCount = isnull(count(*), 0)

    FROM

    aspnet_Membership INNER JOIN

    UserActivationDetails ON aspnet_Membership.UserId = UserActivationDetails.UserId INNER JOIN

    UserGeneral ON aspnet_Membership.UserId = UserGeneral.UserId

    WHERE

    (UserActivationDetails.ActivationStatus = 'False') AND (GETDATE() >= aspnet_Membership.CreateDate + 7) AND (aspnet_Membership.UserId NOT IN

    (SELECT UserId

    FROM ActivationReminders))

    SET @startRowIndex = (@PageIndex * @NoOfRows) + 1;

    WITH ContentEntries AS

    (

    SELECT ROW_NUMBER() OVER (ORDER BY m.CreateDate) AS ROW,

    m.UserId, m.Email,m.CreateDate,U.NickName, U.FirstName, U.LastName, U.ImageThumbnailURL, U.Gender, U.RegistrationType,Days=datediff(dd,m.CreateDate, getdate())

    FROM aspnet_Membership m INNER JOIN

    UserActivationDetails a ON m.UserId = a.UserId INNER JOIN

    UserGeneral U ON m.UserId = U.UserId

    WHERE a.ActivationStatus = 'False' AND (GETDATE() >= m.CreateDate + 7) AND (m.UserId NOT IN

    (SELECT UserId

    FROM ActivationReminders))

    )

    SELECT

    UserId, Email, CreateDate, NickName, FirstName, LastName,

    ImageThumbnailURL, Gender, RegistrationType,Days

    FROM

    ContentEntries

    WHERE

    ROW BETWEEN @startRowIndex AND @startRowIndex + @NoOfRows - 1

    ORDER BY

    @SortBy

    END

  • Please don't cross post!

    It won't reduce the time until you get an answer but it might split or double answers.

    Further discussion please on thread http://www.sqlservercentral.com/Forums/FindPost733638.aspx



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply