How can I Use Row() Over in Procedures

  • Dear All,

    My Procedure is giving the results of 1000 rows in result set..Now they will sending one parameter for display records per page. and as well as number of pages also. How can I use the row() function here..

  • Do you mean Row_Number()?

    If so, do an inner query that gets the whole result-set and uses Row_Number() to number the rows, then an outer query that gets only the rows you want. You can do this with a CTE or an inline query in the From clause.

    If that's what you mean.

    Keep in mind that, unless you set the isolation level to either Repeatable Read or Serializable, and maintain the connection (SPID), you can end up with odd results in that kind of thing because of data changing after a page loads and before the next page is queried.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thank u...

    I need the same, what u said...Thank u Very Much I will implement this...:-)

  • a lame example:

    CREATE PROCEDURE GETSOMEDATA(@Start INT,

    @increment INT)

    AS

    BEGIN

    SET NOCOUNT ON;

    WITH MyQuery

    AS (SELECT ROW_NUMBER() OVER (ORDER BY name) AS RowNumber,

    *

    FROM sysobjects)

    SELECT *

    FROM MyQuery

    WHERE RowNumber BETWEEN @Start AND @Start + @increment

    END --PROC

    GO

    EXEC GETSOMEDATA 10,5

    EXEC GETSOMEDATA 15,5

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • In this Proc Can i set a page number...?

  • SqlSpider... (11/29/2011)


    In this Proc Can i set a page number...?

    @start

  • Thank U Very Much..

    🙂

  • page number is the exact same logic, just handled like this:

    ALTER PROCEDURE GETSOMEDATA(@PageNumber INT)

    AS

    BEGIN

    SET NOCOUNT ON;

    declare @increment int

    SET @increment = 10

    ;WITH MyQuery

    AS (SELECT ROW_NUMBER() OVER (ORDER BY name) AS RowNumber,

    *

    FROM sysobjects)

    SELECT *

    FROM MyQuery

    WHERE RowNumber BETWEEN ((@PageNumber -1) * @increment) AND ((@PageNumber) * @increment)

    END --PROC

    GO

    EXEC GETSOMEDATA 1

    EXEC GETSOMEDATA 2

    EXEC GETSOMEDATA 3

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 8 posts - 1 through 7 (of 7 total)

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