question

Marcos Cruz avatar image
Marcos Cruz asked

Paging in SQL Server

Hi,

Please, I'm developing a web application and I have two tables that could be many many records. I need make a query search (stored procedure) that limit the result to 20 records for page.

I think that I must use ROW_NUMBER ou create a temp table, but the fact is that I don´t know why.

Can you help me?

Thanks

Marcos Cruz, São Paulo, Brasil

SELECT     s.SetorAtividadeId                    
          ,s.SetorAtividade                    
          ,rAtividade.RamoAtividadeId                    
          ,rAtividade.RamoAtividade                    
FROM                    
           SetorAtividade As s                    
LEFT OUTER JOIN                    
           RamoAtividade as r                    
ON         s.SetorAtividadeId = r.SetorAtividadeId                    
WHERE                         
          (s.SetorAtividade LIKE @param)                    
ORDER BY                     
           sAtividade.SetorAtividade                    
          ,r.RamoAtividade                    
sql-server-2005paging
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Madhivanan avatar image
Madhivanan answered

Also refer method 4 http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx Here is the code

Declare @t table(item varchar(100), price float)             
insert into @t            
select 'item1', 20000 union all            
select 'item1', 20000 union all            
select 'item1', 20700 union all            
select 'item2', 57600 union all            
select 'item2', 80120 union all            
select 'item3', 89760 union all            
select 'item3', 87680 union all            
select 'item4', 87680 union all            
select 'item4', 43220 union all            
select 'item4', 43220             
            
Select item,price from            
(            
select row_number() over(order by item) as row_number, * from @t            
) T            
where row_number between 1 and 5            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

craigvn avatar image
craigvn answered

You can do paging like this in SQL 2005+

SELECT TOP 40 StudentId, StudentName            
FROM (            
    SELECT ROW_NUMBER() OVER(ORDER BY StudentName ASC) as row,             
    	StudentId, StudentName             
    FROM (            
    	SELECT [Student].[StudentId] AS StudentId, [Student].[StudentName] AS StudentName			            
    	FROM Student Student             
    ) query             
) page             
WHERE page.row > 0             
ORDER BY StudentName ASC            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mrdenny avatar image
mrdenny answered

We talked about paging result sets over here.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.