Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SP for paging

Author  Topic 

cesark
Posting Yak Master

215 Posts

Posted - 2005-03-24 : 10:55:59
Is this SP for paging well written? I mean if ‘where’ clauses are in the correct places? If @TotalRegisters is set properly? Or if is there repeated code unnecessarily?


USE market
GO
ALTER PROCEDURE offers_list
@CurrentPage int, @PageSize int, @TotalRegisters int output
As

BEGIN
SET NOCOUNT ON

DECLARE
@Max_row int,
@First_row int

SET @Max_row = @PageSize * @CurrentPage
SET @First_row = @Max_row - (@PageSize - 1)


Declare @Date As datetime
Declare @Id As bigint
Declare @City As varchar(100)


SET ROWCOUNT @First_row

Select @Date = TheDate, @Id = Offer_id, @City = city.City_name
From Offers As Offe
JOIN Users As us
On Offe.User_num = us.User_id
JOIN Cities As city
On Offe.city_num = city.City_id
Where city.City_name = 'Paris'
Order by TheDate Desc, Offer_id Desc


SELECT @TotalRegisters = COUNT(*) FROM Offers As Offe
JOIN Users As us
On Offe.User_num = us.User_id
JOIN Cities As city
On Offe.city_num = city.City_id
Where city.City_name = ‘Paris'

SET ROWCOUNT @PageSize



Select Offer_id, TheDate, Title, us.Company_name, city.City_name
From Offers As Offe
JOIN Users As us
On Offe.User_num = us.User_id
JOIN Cities As city
On Offe.city_num = city.City_id

Where ((TheDate < @Date) Or (TheDate = @Date And Offer_id <= @Id))
And city.City_name = 'Paris'
Order by TheDate Desc, Offer_id Desc
SET ROWCOUNT 0

SET NOCOUNT OFF
END
GO


Thank you,
Cesar

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2005-03-24 : 16:17:29
[code]SELECT @TotalRegisters = COUNT(*) FROM Offers As Offe
JOIN Users As us
On Offe.User_num = us.User_id
JOIN Cities As city
On Offe.city_num = city.City_id
Where city.City_name = ‘Paris'[/code]
I think it should be placed AFTER the final SET ROWCOUNT 0
Go to Top of Page

cesark
Posting Yak Master

215 Posts

Posted - 2005-03-29 : 10:06:39
Ok Stoad, thank you
Go to Top of Page

cesark
Posting Yak Master

215 Posts

Posted - 2005-03-29 : 10:26:17
Something strange is happening when I run this SP. If in the first page in the app there is less records than the @PageSize (for example: @PageSize = 20, and total records found = 2) then the page 1 displays the two records (ok) but in the second page is displayed the last record of the previous page, when It must be none. Why this happen?

Thanks
Go to Top of Page
   

- Advertisement -