Paging in SQL Server 2012

Forrards
March 31, 2023

Paging is one of the most highly required features during application development process. Microsoft has introduced various features since the times of SQL Server 2000 to the latest version of SQL Server 2014. In SQL Server 2012 a new feature was added to the ORDER BY clause. It allows to write more efficient query since the query optimization standpoint and makes work easier for anyone who writes in T-SQL and needs data paging implemented.

Before creating script for paging, you should create a set of test data. You can find the script for creating temporary table and inserting test data to the temporary table below:

1) Creating temporary table #Tmp:

CREATE TABLE #Tmp( ID int NOT NULL IDENTITY(1,1), Name nvarchar(50) NOT NULL );
GO

2) Inserting 100 rows into the temporary table:

INSERT INTO #Tmp (Name) VALUES ('Example No' + CONVERT(VARCHAR,ISNULL(@@IDENTITY, 0)))
GO 100

Test data is now ready and paging script can be executed. Take a look at the below code snippet:

DECLARE @PageNumber AS INT ,
@RowspPage AS INT
SET @PageNumber = 1 SET @RowspPage = 10
SELECT ID, Name
FROM #Tmp
ORDER BY ID OFFSET ((@PageNumber - 1) * @RowspPage)
ROWS FETCH NEXT @RowspPage ROWS ONLY;

This example utilizes the OFFSET function. It is important to mention that the first record returned will be in the "zero" position. In this case you can define the @PageNumber variable with value 1 and the @RowspPage variable with value 10. So that you receive the following data:

- the first row in the 1 position

- the last row in the 10 position

Paging SQL Server 2012

Forrards
March 31, 2023
Share
Recent articles:
Database consultancy: SaaS database considerations
March 31, 2023

Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.

More
Database consultancy: tree-like structure
March 31, 2023

Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:

More
Database consultancy: Hierarchy in MS SQL Server
March 31, 2023

In our time of greatest prevalence of databases were relational databases, which are the main targets of the tables and the relationships between them. Tables can solve most problems for data storage and manipulation. But in the real world entity requiring storage is not always presented in a tabular form. One of these very common types of data structures other than the table is a tree structure, where each data element is the parent and the offspring. An example of such a structure may be the structure of state enterprises, which is headed by the director (the root of the tree), his deputies, heads of departments from which are subject to certain deputies, employees of departments, which are subject to the rulers.

More

Subscribe to our blog

    

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Database consultancy: SaaS database considerations

Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.

March 31, 2023
Database consultancy: tree-like structure

Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:

March 31, 2023

Discuss your project with us

    

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.