SELECT certain number of rows

  • Indexed multi-table view... multi-table SELECT... which one will you call UPDATEable? Neither in all likely hood. An update would have to be something separate... paging isn't used to update... πŸ˜‰

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (3/13/2008)


    Indexed multi-table view... multi-table SELECT... which one will you call UPDATEable? Neither in all likely hood. An update would have to be something separate... paging isn't used to update... πŸ˜‰

    Agreed. Just making sure:).

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • all possible ways are discussed above but think about

    not in using top

    select top 10 *

    from table

    where id not in (select top 20 id from table)

    and cte

    WITH Folder (IsUserFolder ,ParentFolderRowGUID ,RowGUID)

    AS

    (

    SELECT *

    FROM dbo.tblFolder AS P

    whereRowGUID = @pRowGUID

    UNION ALL

    SELECT P.*

    FROM dbo.tblFolder AS P

    INNER JOIN Folder AS d

    ON p.RowGUID = d.ParentFolderRowGUID

    )

    select top 1 * FROM Folder

    where class filters the records

  • Using Adam's sample data:

    -- Top 21 TO 30 (less than one second)

    SELECT TOP 10 * FROM (

    SELECT TOP 30 * FROM dbo.TestData ORDER BY RowNum

    ) t ORDER BY RowNum DESC

    -- Top 999,981 to 999,990 (one second)

    SELECT TOP 10 * FROM (

    SELECT TOP 999990 * FROM dbo.TestData ORDER BY RowNum

    ) t ORDER BY RowNum DESC

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 4 posts - 16 through 18 (of 18 total)

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