15
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLServerでLIMIT句を利用する

Last updated at Posted at 2017-03-13

DBから指定の数だけレコードを引っ張ってくる際に、MySQLではLIMIT句を使えば簡単にできるが
SQLServerはひと工夫が必要。

/* テーブルAからname順に最初の10件のデータを取得する */
SELECT TOP 10 * FROM テーブルA ORDER BY name
 
/* テーブルAからname順に11件目~20件目のデータを取得する */
SELECT * FROM
(SELECT name,ROW_NUMBER() OVER (ORDER BY name) AS record_num FROM テーブルA) AS t
WHERE record_num BETWEEN 11 AND 20
ORDER BY record_num

頭から表示させるのは難しくないが、途中から表示させるのは難しい。

15
15
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?