Hi,Now I am paging through the results of my ASP.NET application with this SP:USE marketGOALTER PROCEDURE offers_list@CurrentPage int, @PageSize int, @TotalRecords int outputAsCREATE TABLE #tempTable( Id int IDENTITY PRIMARY KEY, Offer_id bigint, Date datetime, Offer_Title varchar(100), Company_name varchar(100), City_name varchar(100) )INSERT INTO #tempTable(Offer_id, Date, Offer_Title, Company_name, City_name )Select Offer_id, Date, Offer_Title, user.Company_name, city.City_nameFrom Offers As offeJOIN Users As userOn offe.User_num = user.User_idJOIN Cities As cityOn offe.city_num = city.City_idOrder by Date DESCDeclare @FirstReg int, @LastReg intSelect @FirstReg = (@CurrentPage - 1) * @PageSize Select @LastReg = (@CurrentPage * @PageSize) + 1SELECT Offer_id, Date, Offer_Title, Company_name, City_name FROM #tempTableWHEREId > @FirstReg AND Id < @LastRegSELECT @TotalRecords = COUNT(*) FROM #tempTable DROP TABLE #tempTable GO
And to avoid performance problems due to many users at the same time creating and dropping temp tables with many registers every time, I want to achieve the same results (performance) that now my SP produces but without using a temporary table. I found a code sample that I think does what I am looking for. The problem is that I don’ t know how to adapt my SP to this other. The technique is called ROWCOUNT.Here is the sample I found in this page: http://www.aspfaq.com/show.asp?id=2120 CREATE PROCEDURE SampleCDs_Paging_Rowcount @pagenum INT = 1, @perpage INT = 50 AS BEGIN SET NOCOUNT ON DECLARE @ubound INT, @lbound INT, @pages INT, @rows INT SELECT @rows = COUNT(*), @pages = COUNT(*) / @perpage FROM SampleCDs WITH (NOLOCK) IF @rows % @perpage != 0 SET @pages = @pages + 1 IF @pagenum < 1 SET @pagenum = 1 IF @pagenum > @pages SET @pagenum = @pages SET @ubound = @perpage * @pagenum SET @lbound = @ubound - (@perpage - 1) SELECT CurrentPage = @pagenum, TotalPages = @pages, TotalRows = @rows -- this method determines the string values -- for the first desired row, then sets the -- rowcount to get it, plus the next n rows DECLARE @aname VARCHAR(64), @title VARCHAR(64) SET ROWCOUNT @lbound SELECT @aname = ArtistName, @title = Title FROM SampleCDs WITH (NOLOCK) ORDER BY ArtistName, Title SET ROWCOUNT @perPage SELECT ArtistName, Title FROM SampleCDs WITH (NOLOCK) WHERE ArtistName + '~' + Title >= @aname + '~' + @title ORDER BY ArtistName, Title SET ROWCOUNT 0 END GO
Does anyone know how can I adapt my initial SP to this technique? And, what do you think about it?Thank you