How to select the nth row in a SQL database table?

后端 未结 30 2553
执笔经年
执笔经年 2020-11-22 06:06

I\'m interested in learning some (ideally) database agnostic ways of selecting the nth row from a database table. It would also be interesting to see how this can b

30条回答
  •  醉话见心
    2020-11-22 06:49

    For SQL Server, a generic way to go by row number is as such:

    SET ROWCOUNT @row --@row = the row number you wish to work on.
    

    For Example:

    set rowcount 20   --sets row to 20th row
    
    select meat, cheese from dbo.sandwich --select columns from table at 20th row
    
    set rowcount 0   --sets rowcount back to all rows
    

    This will return the 20th row's information. Be sure to put in the rowcount 0 afterward.

提交回复
热议问题