GENERAL ARTICLESPROGRAMMINGSQLT-SQL

SQL Server OFFSET FETCH (Pagination)

“OFFSET FETCH” terimi, SQL Server’da bir sorgunun sonuç kümesinden belli bir başlangıç noktasından itibaren kaç satır alınacağını belirlemek için kullanılan bir yapıdır. Bu yapı, genellikle sonuçları sayfalama veya belirli bir veri aralığını getirme amacıyla kullanılır. İşte temel kullanım şekli:

SELECT sütun1, sütun2, …
FROM tablo_adı
ORDER BY sıralama_sütunu
OFFSET atlama_satırları FETCH NEXT alınacak_satırlar ROWS ONLY;

 

Burada:

  • sütun1, sütun2, ...: Sonuçlarda görünen sütunlar.
  • tablo_adı: Verilerin alınacağı tablo.
  • sıralama_sütunu: Sonuçları nasıl sıralayacağınızı belirten sütun.
  • atlama_satırları: Başlangıç noktasından kaç satır atlamanız gerektiği.
  • alınacak_satırlar: Kaç satırın alınacağı.
  • ROWS ONLY: Sadece satır sayısını belirtir, sayfa sayısını değil.

Örneğin, şu sorgu 10 satır atlama ve sonrasında 5 satır almayı belirtir:

 

SELECT Ad, Soyad
FROM Musteriler
ORDER BY Soyad
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;

 

Bu sorgu, “Musteriler” tablosundaki adları soyad sırasına göre sıralayacak, ilk 10 satırı atlacak ve ardından 5 satırı getirecektir. Bu şekilde, verilerin belirli bir aralığını alarak sayfalama yapabilirsiniz.

 

Demystifying OFFSET FETCH in SQL Server: Paging Results Made Easy

In the world of SQL Server querying, the OFFSET FETCH clause plays a vital role in retrieving specific subsets of data from a result set. This technique is commonly used for paging through large datasets or fetching data within a defined range. In this blog post, we’ll dive deep into the OFFSET FETCH clause and explore its practical applications.

 

The OFFSET FETCH clause is employed to skip a certain number of rows from the beginning of a result set and then fetch a specified number of rows. This operation is typically used for implementing pagination, where you need to display a certain number of records per page. The basic syntax of the clause is as follows:

 

SELECT column1, column2, …
FROM table_name
ORDER BY column_name
OFFSET offset_rows FETCH NEXT fetch_rows ROWS ONLY;

 

Here’s a breakdown of the components:

  • column1, column2, ...: The columns you want to select.
  • table_name: The table from which data is retrieved.
  • column_name: The column used for sorting the results.
  • offset_rows: The number of rows to skip from the beginning.
  • fetch_rows: The number of rows to fetch.
  • ROWS ONLY: Specifies that only the number of rows is provided, not the page numbers.

Let’s consider a practical example. Suppose we have a “Customers” table, and we want to retrieve a specific page of customer records. Here’s how you can use the OFFSET FETCH clause:

 

SELECT FirstName, LastName
FROM Customers
ORDER BY LastName
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;

 

In this example, the query will order the names in the “Customers” table by last name, skip the first 10 rows, and then fetch the next 5 rows. This is particularly useful for implementing pagination in web applications.

References:

  • Microsoft Docs. (URL to official documentation)
  • SQL Server Central. (URL to relevant tutorial or article)
Paylaş

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir