Row Offset in SQL Server

后端 未结 16 2394
醉酒成梦
醉酒成梦 2020-11-22 05:53

Is there any way in SQL Server to get the results starting at a given offset? For example, in another type of SQL database, it\'s possible to do:

SELECT * FR         


        
16条回答
  •  [愿得一人]
    2020-11-22 06:16

    I've been searching for this answer for a while now (for generic queries) and found out another way of doing it on SQL Server 2000+ using ROWCOUNT and cursors and without TOP or any temporary table.

    Using the SET ROWCOUNT [OFFSET+LIMIT] you can limit the results, and with cursors, go directly to the row you wish, then loop 'till the end.

    So your query would be like this:

    SET ROWCOUNT 75 -- (50 + 25)
    DECLARE MyCursor SCROLL CURSOR FOR SELECT * FROM pessoas
    OPEN MyCursor
    FETCH ABSOLUTE 50 FROM MyCursor -- OFFSET
    WHILE @@FETCH_STATUS = 0 BEGIN
        FETCH next FROM MyCursor
    END
    CLOSE MyCursor
    DEALLOCATE MyCursor
    SET ROWCOUNT 0
    

提交回复
热议问题