SoFunction
Updated on 2025-04-08

A paginated stored procedure I wrote before, I accidentally dug out just now

CREATE PROCEDURE GoalerPageSp
@IntPageSize int,
@IntCurrPage int,
@strFields nvarchar(2000),
@strTable varchar(200),
@strWhere varchar(800),
@strOrderType varchar(200),
@strKeyField varchar(50)
AS
SET NOCOUNT ON
DECLARE @tmpSQL nvarchar(4000)--Storing dynamic SQL statements
DECLARE @tmpWhere varchar(800)
DECLARE @tmpAndWhere varchar(800)-- Used for query conditions on the top of page N (>1)
DECLARE @tmpOrder varchar(200)
DECLARE @tmpD_X varchar(2)
DECLARE @tmpMin_MAX varchar(3)

--Set the conditions-
IF @strWhere IS NULL OR RTRIM(@strWhere)=''
    BEGIN --No query conditions
        SET @tmpWhere=''
        SET @tmpAndWhere=''
    END 
ELSE 
    BEGIN --There are query conditions
        SET @tmpWhere=' WHERE '+@strWhere
        SET @tmpAndWhere=' AND '+@strWhere
    END 

--Set the sort-
IF @strOrderType != 0
    BEGIN--Inverse order 
        SET @tmpD_X = '<'
        SET @tmpMin_MAX = 'MIN'
        SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' DESC'
    END 
ELSE 
    BEGIN 
        SET @tmpD_X = '>'
        SET @tmpMin_MAX = 'MAX'
        SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' ASC'
    END 
--SQL query-
IF @IntCurrPage=1
    Set @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder
ELSE
    SET @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (SELECT '+@tmpMin_MAX+'('+@strKeyField+') FROM (SELECT TOP '+CAST(@IntPageSize*(@IntCurrPage-1) AS VARCHAR)+' '+@strKeyField+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') AS T))'+@tmpAndWhere+' '+@tmpOrder
EXEC(@tmpSQL)
GO


Calling method:
IntPageSize=20
strTable=" [TableName] "    'Data table name
strFields=" Field1,Field2,Field3,Field4 "    'The column name that needs to be read
strKeyField="Field1"    'Primary key: Here is assumed that Field1 is the primary key
strWhere=""    'Condition: FieldA='b'
strOrderType=1    'Sorting method: 1 is in reverse order, 0 is in order

CurrPage=("Page")
IF(CurrPage<>"" And Isnumeric(CurrPage))THEN
    CurrPage=CLNG(CurrPage)   
    IF(CurrPage<1)THEN CurrPage=1                   
ELSE
    CurrPage=1
END IF

IF strWhere<>"" THEN
    tmpWhere=" WHERE "&strWhere
ELSE 
    tmpWhere=""
END IF

IF(SESSION("RecCount")<>"")THEN
    IF(SESSION("strWhere")<>strWhere)THEN
        RecCount=("SELECT COUNT("&strKeyField&") FROM "&strTable&tmpWhere)(0)
        SESSION("RecCount")=RecCount
        SESSION("strWhere")=strWhere
    ELSE
        RecCount=SESSION("RecCount")
    END IF
ELSE
    RecCount=("SELECT COUNT(*) FROM "&strTable&tmpWhere)(0)
    SESSION("RecCount")=RecCount
    SESSION("strWhere")=strWhere
END IF

IF(RecCount MOD IntPageSize <>0)THEN
    IntPageCount=INT(RecCount/IntPageSize)+1
ELSE
    IntPageCount=RecCount/IntPageSize
END IF

SET Cmd=("") 
=4 
SET =Conn 
="GoalerPageSp" 
 ("@IntPageSize",4,1,4,IntPageSize)
 ("@IntCurrPage",4,1,4,CurrPage)
 ("@strFields",200,1,2000,strFields)
 ("@strTable",200,1,200,strTable)
 ("@strWhere",200,1,800,strWhere)
 ("@strOrderType",4,1,4,strOrderType)
 ("@strKeyField",200,1,50,strKeyField)
SET RS=()
IF RecCount<1 THEN
("No record")
ELSE
    GetRecord=(IntPageSize)
    For i=0 To Ubound(GetRecord,2)
        (GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))    '...Output content
    NEXT
    GetRecord=Null
END IF
SET RS=NOTHING


If you are useful, please debug it slowly. The total records are taken by ASP and stored in SESSION. If you count the total records every time, it will be very time-consuming. Of course, if you want toStored proceduresIt is also OK to get the total record and the total number of pages in it and then return it. The following is the code:
--get the total number of records-
SET @tmpSQL='SELECT @getRecordCounts=COUNT('+@strKeyField+') FROM '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts int output',@getRecordCounts OUTPUT

--Get the total number of pages-
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
IF @getRecordCounts<=@IntPageSize
    SET @getPageCounts=1
ELSE
BEGIN
    IF @tempFolatNumber != 0
        SET @getPageCounts=(@getRecordCounts/@IntPageSize)+1
    ELSE
        SET @getPageCounts=(@getRecordCounts/@IntPageSize)
END


Don't forget to return the definition parameters:
@getRecordCounts int output,--Return to total records
@getPageCounts int output--Return to the total number of pages