Fast paging technic MS SQL2005

Fast paging technic MS SQL2005

In MySQL it’s easy to page with SELECT … LIMIT … , but in MSSQL we must do something below.

In MSSQL 2000 we used to do paging either by dynamic sql or by some advanced techniques like the example with rowcount. In MSSQL 2005 with the introduction of ROW_NUMBER function life is a lot easier.

DECLARE @PageNum AS INT;
DECLARE @PageSize AS INT;
SET @PageNum = 2;
SET @PageSize = 10;

WITH OrdersRN AS
(
    SELECT ROW_NUMBER() OVER(ORDER BY OrderDate, OrderID) AS RowNum
          ,OrderID
          ,OrderDate
          ,CustomerID
          ,EmployeeID
      FROM dbo.Orders
)

SELECT *
  FROM OrdersRN
 WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1
                  AND @PageNum * @PageSize
 ORDER BY OrderDate
         ,OrderID;

Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 

Giới thiệu 10 Drupal modules tuyệt với khi kết hợp với Views

This article looks at 10 modules that integrate with Views and that I think people should know about

Google Voice App Review

Google Voice App Review

Finally ending the long wait, the Google Voice has rolled out in its full glamour from the Google factory. It is the like the apple of the Apple’s eye. One of the most downloaded versions in the Apple’s App Store, this Google Voice application is a cult in its own right.

Google Sandbox

Google Sandbox bộ lọc website mới

Vào tháng 5 năm 2004, Google cài đặt một bộ lọc mới được giới webmaster gọi là Sandbox.

Công ty diệt chuột T&C

 

Diet con trung