How to skip the first n rows in sql query

前端 未结 11 1009
清歌不尽
清歌不尽 2020-12-09 01:40

I want to fire a Query \"SELECT * FROM TABLE\" but select only from row N+1. Any idea on how to do this?

11条回答
  •  情书的邮戳
    2020-12-09 02:27

    For SQL Server 2012 and later versions, the best method is @MajidBasirati's answer.

    I also loved @CarlosToledo's answer, it's not limited to any SQL Server version but it's missing Order By Clauses. Without them, it may return wrong results.

    For SQL Server 2008 and later I would use Common Table Expressions for better performance.

    -- This example omits first 10 records and select next 5 records
    ;WITH MyCTE(Id) as
    (
        SELECT TOP (10) Id 
        FROM MY_TABLE
        ORDER BY Id
    )
    SELECT TOP (5) * 
    FROM MY_TABLE
        INNER JOIN MyCTE ON (MyCTE.Id <> MY_TABLE.Id) 
    ORDER BY Id
    

提交回复
热议问题