with wcte as (
Select Row_number()over (order by a.name) as RNO
from sys.columns a, sys.columns b, sys.columns c
) Select rno , 'Name: ' + CAST(rno as varchar(10)) as Name
into tblTest
from wcte where RNO <= 1000000
Add primary key on RNO column through the designer. This will add a clustered index on this column.
Now, lets write the query for the SQL Server 2005 / 2008 / 2008 R2 using CTE;
;With wcte as (
Select Row_Number() over (Order by Rno) Rno2,* from tblTest WHERE Name is Not Null
) Select * from wcte where rno2 > 899979 and rno2 < 900000
order by rno
Now lets write the query for SQL Server 2012 paging;
Declare @vPageNo int
Declare @vOffset int
Declare @vPageSize int
Set @vPageNo = 45000
Set @vPageSize = 20
Set @vOffset = (@vPageNo-1) * @vPageSize
SELECT *
FROM tblTest
WHERE Name is Not Null
ORDER BY rno
OFFSET @vOffset ROWS
FETCH NEXT @vPageSize ROWS ONLY
I have added the variables for passing OFFSET and PAGESIZE. This gives an additional hint for passing these from the front end as Input parameters of stored procedure.
Results might be different if you apply the same scenario directly on the clustered indexed column (RNO of tbltest in my case) instead of applying Row_Number() function. This is not the real time case as you never have IDs in sequence in you db tables as sequences break when you delete or migrate. More importantly, you apply paging on queries with WHERE clause. And you cannot use ID columns for paging in any case with WHERE Clause.
No comments:
Post a Comment