Sqlserver can also be Based on Pseudo-column paging!

Source: Internet
Author: User
Like Oracle, SQL Server 2005 is followed by the row_number () pseudo-column function. You can use a subquery to implement data paging.

Orcale paging: Let's take a look at this article. Let's take a look at how Oracle Stored Procedure dynamic SQL queries get query pages!

First, give it a try.

 SELECT * FROM 
(
SELECT ROW_NUMBER() OVER (ORDER BY id desc)AS Row, * from test
)   as temp where Row between 1 and 10

It seems okay ......

Rewrite it:

declare 
@v_index int,
@v_size int
begin
set @v_index = 1;
set @v_size = 10;
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY id desc)AS Row, * from test
)
as temp where Row
between (@v_index-1)*@v_size+1         and (@v_index-1)*@v_size+@v_size
end;

Use the with statement to rewrite the statement and simulate the stored procedure written by others.

create procedure proc_get_test_list 
(
@p_index int,
@p_size int
)
as
begin

with temptb as
(
select row_number() over (order by id desc) as row, * from test
)

select * from temptb where row between

(@p_index-1)*@p_size+1 and (@p_index-1)*@p_size+@p_size

end

It is said that writing in this way can improve the statement efficiency.

Under the condition of 1000 normal query statements, the response time is almost ignored!

Test the stored procedure:

It should not matter whether it is up or down... continue to study later!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.