SQL取中間筆數

  • 750
  • 0

SQL取中間筆數的語法

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5e6c05cb-1129-4bfe-a45b-e8cb2892e240/how-select-range-10-to-20-record-of-sql-bank?forum=sqldataaccess

 

SQL Server 2012以上的版本

SELECT * 
	FROM TableName
	ORDER BY h
	OFFSET 10 ROWS
	FETCH NEXT 10 ROWS ONLY;

SQL Server 2012以下的版本

SELECT * FROM
  (SELECT ROW_NUMBER() OVER (ORDER BY h) AS MyRowNumber, *
  FROM TableName) tbl
WHERE MyRowNumber BETWEEN 10 AND 20;