Patriotec Patriotec - 7 months ago 29
SQL Question

Need to convert paging stored procedure for large data sets using SQL Server 2005

Right now the code is (to keep things simple I didn't use the ADO Command Object yet to prevent SQL injection).

I have a large dataset. I'm using getrows method which is awesome to retrieve the recordsets. I can't find a similar stored procedure to retrieve records based on which page you are own within the browser.

Basically the sql retrieves 10 recordsets relative to the page the clients browser is on. It gets the page value from the CurrPage value. I handle the first page and last page with server side code so there arent any errors.

Any help is appreciated.

If IsEmpty(Request.Querystring("pg")) then
CurrPage = 1
Else
CurrPage = Cint(Request.Querystring("pg"))
End If

RSPrevPage = CurrPage -1
RSNextPage = CurrPage + 1

SQL = "SELECT gallerypublic.img, gallerypublic.galleryID, blahblahblah FROM gallerypublic INNER JOIN GalleryPublicCat ON gallerypublic.publicgallerycatid = GalleryPublicCat.pubcatID INNER JOIN userbase ON gallerypublic.userid = userbase.userid Order by galleryid desc"

Set rsFeed = Server.CreateObject("ADODB.Recordset")
rsFeed.Open sql, Conn, adOpenKeyset, adLockReadOnly
rsFeed.PageSize = 10
rsFeed.AbsolutePage = CurrPage
arrFeed = rsFeed.getrows(10)
intPageCount = rsFeed.PageCount
rsFeed.close
set rsFeed = Nothing


I found this stored procedure on http://www.aspfaqs.com/webtech/042606-1.shtml but I can't translate it to my needs.

TIA

After a little trial and error, here's the working code:

Server Side:

If IsEmpty(Request.Querystring("pg")) then
CurrPage = 1
Else
CurrPage = Cint(Request.Querystring("pg"))
End If

RSPrevPage = CurrPage -1
RSNextPage = CurrPage + 1

pgSize = 10

Set objCommandSec = CreateObject("ADODB.Command")
objCommandSec.ActiveConnection = Conn

With objCommandSec
Set .ActiveConnection = Conn
.CommandType = adCmdStoredProc
.CommandText = "spPageDef"
.Parameters.Append .CreateParameter("@PageNum", 200, 1, 255, CurrPage)
.Parameters.Append .CreateParameter("@PageSize", 200, 1, 255, pgSize)
.Parameters.Append .CreateParameter("@TotalRowsNum", adInteger, adParamReturnValue)

Set rsFeed = objCommandSec.Execute
arrFeed = rsFeed.getrows()
rsFeed.close
set rsFeed = nothing

intPageCount = cLng((.Parameters(2).value/pgSize))

End With


Stored Procedure:

@PageNum int,
@PageSize int,
@TotalRowsNum int output

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Use ROW_NUMBER function

WITH DefaultEntries As
(
SELECT
g.img, g.galleryID, g.viewed, g.votes, g.rate, g.created, blahblah,
'RowNumber' = ROW_NUMBER() OVER(ORDER BY galleryid DESC)

FROM gallerypublic AS g INNER JOIN GalleryPublicCat AS gpc ON g.publicgallerycatid = gpc.pubcatID INNER JOIN userbase AS u ON g.userid = u.userid
)

-- Query result

SELECT *
FROM DefaultEntries
WHERE RowNumber BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize
ORDER BY galleryid DESC

SELECT @TotalRowsNum = count(galleryid)
FROM gallerypublic AS g INNER JOIN GalleryPublicCat AS gpc ON g.publicgallerycatid = gpc.pubcatID INNER JOIN userbase AS u ON g.userid = u.userid

END


Thanks to all who helped out and pointed me in the correct direction

Answer

Stored Procedure

    @PageNum int,
    @PageSize int,
    @TotalRowsNum int output

    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from

        -- interfering with SELECT statements.

        SET NOCOUNT ON;

        -- Use ROW_NUMBER function

        WITH DefaultEntries As
        (
            SELECT  
g.img, g.galleryID, g.viewed, g.votes, g.rate, g.created, blahblah,
'RowNumber' = ROW_NUMBER() OVER(ORDER BY galleryid DESC)

        FROM gallerypublic AS g INNER JOIN GalleryPublicCat AS gpc ON g.publicgallerycatid = gpc.pubcatID INNER JOIN userbase AS u ON g.userid = u.userid
        )

        -- Query result

        SELECT * 
        FROM DefaultEntries
        WHERE RowNumber BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize             
        ORDER BY galleryid DESC

    SELECT @TotalRowsNum = count(galleryid) 
    FROM gallerypublic AS g INNER JOIN GalleryPublicCat AS gpc ON g.publicgallerycatid = gpc.pubcatID INNER JOIN userbase AS u ON g.userid = u.userid

    END

Server Side

If IsEmpty(Request.Querystring("pg")) then
CurrPage = 1
Else
CurrPage = Cint(Request.Querystring("pg"))
End If

RSPrevPage = CurrPage -1
RSNextPage = CurrPage + 1

pgSize = 10

Set objCommandSec = CreateObject("ADODB.Command") 
objCommandSec.ActiveConnection = Conn

With objCommandSec
Set .ActiveConnection = Conn
.CommandType = adCmdStoredProc
.CommandText = "spPageDef"
.Parameters.Append .CreateParameter("@PageNum", 200, 1, 255, CurrPage)
.Parameters.Append .CreateParameter("@PageSize", 200, 1, 255, pgSize)
.Parameters.Append .CreateParameter("@TotalRowsNum", adInteger, adParamReturnValue)

Set rsFeed = objCommandSec.Execute
arrFeed = rsFeed.getrows()
rsFeed.close
set rsFeed = nothing

intPageCount = cLng((.Parameters(2).value/pgSize))

End With