CRÉER UNE PROCÉDURE GoalerPageSp
@IntPageSize entier,
@IntCurrPage entier,
@strFields nvarchar(2000),
@strTablevarchar(200),
@strOù varchar(800),
@strOrderTypevarchar(200),
@strKeyFieldvarchar(50)
COMME
DÉFINIR NOCOUNT ON
DECLARE @tmpSQL nvarchar(4000)--Stockage des instructions SQL dynamiques
DÉCLARER @tmpWhere varchar(800)
DECLARE @tmpAndWhere varchar(800) --utilisé pour les conditions de requête à la page N (>1)
DÉCLARER @tmpOrder varchar(200)
DÉCLARE @tmpD_X varchar(2)
DÉCLARE @tmpMin_MAX varchar(3)
--Définir les conditions--
SI @strWhere EST NULL OU RTRIM (@strWhere)=''
BEGIN--Aucune condition de requête
SET @tmpWhere=''
SET @tmpAndWhere=''
FIN
AUTRE
BEGIN--Il existe des conditions de requête
SET @tmpWhere=' OÙ '+@strOù
SET @tmpAndWhere=' ET '+@strWhere
FIN
--Définir le tri--
SI @strOrderType != 0
COMMENCER – ordre inverse
FIXER @tmpD_X = '<'
FIXER @tmpMin_MAX = 'MIN'
SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' DESC'
FIN
AUTRE
COMMENCER
FIXER @tmpD_X = '>'
FIXER @tmpMin_MAX = 'MAX'
SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' ASC'
FIN
--Requête SQL--
SI @IntCurrPage=1
Définir @tmpSQL='SELECT TOP '+CAST (@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder
AUTRE
SET @tmpSQL='SELECT TOP '+CAST (@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (SELECT '+@tmpMin_MAX+'(' +@strKeyField+') FROM (SÉLECTIONNER HAUT '+CAST (@IntPageSize*(@IntCurrPage-1) AS VARCHAR)+' '+@strKeyField+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') AS T))'+@tmpAndWhere+' ' +@tmpCommande
EXEC (@tmpSQL)
ALLER
Méthode d'appel :
IntPageSize = 20
strTable= [TableName] 'Nom de la table de données
strFields= Field1,Field2,Field3,Field4 'Les noms de colonnes à lire
strKeyField=Field1'primary key : ici, on suppose que Field1 est la clé primaire
strWhere='Condition : ChampA='b'
strOrderType=1'Méthode de tri : 1 est l'ordre inverse, 0 est l'ordre séquentiel
CurrPage=Request.QueryString(Page)
SI(CurrPage<> Et Isnumeric(CurrPage))ALORS
Page Curr = CLNG (Page Curr)
SI(CurrPage<1)ALORS CurrPage=1
AUTRE
Page Courante=1
FIN SI
SI strOù <> ALORS
tmpWhere= OÙ &strOù
AUTRE
tmpOù=
FIN SI
SI(SESSION(RecCount)<>)ALORS
SI(SESSION(strOù)<>strOù)ALORS
RecCount=Conn.Execute(SELECT COUNT(&strKeyField&) FROM &strTable&tmpWhere)(0)
SESSION(RecCount)=RecCount
SESSION(strOù)=strOù
AUTRE
CompteEnregistrement=SESSION(CompteEnregistrement)
FIN SI
AUTRE
RecCount=Conn.Execute(SELECT COUNT(*) FROM &strTable&tmpWhere)(0)
SESSION(RecCount)=RecCount
SESSION(strOù)=strOù
FIN SI
SI (RecCount MOD IntPageSize <>0) ALORS
IntPageCount=INT(RecCount/IntPageSize)+1
AUTRE
IntPageCount=RecCount/IntPageSize
FIN SI
SET Cmd=Serveur.CreateObject(Adodb.Command)
Cmd.CommandType=4
SET Cmd.ActiveConnection = Conn
Cmd.CommandText=GoalerPageSp
Cmd.Parameters.Append Cmd.CreateParameter (@IntPageSize,4,1,4,IntPageSize)
Cmd.Parameters.Append Cmd.CreateParameter (@IntCurrPage,4,1,4,CurrPage)
Cmd.Parameters.Append Cmd.CreateParameter (@strFields,200,1,2000,strFields)
Cmd.Parameters.Append Cmd.CreateParameter (@strTable,200,1,200,strTable)
Cmd.Parameters.Append Cmd.CreateParameter (@strWhere,200,1,800,strWhere)
Cmd.Parameters.Append Cmd.CreateParameter (@strOrderType,4,1,4,strOrderType)
Cmd.Parameters.Append Cmd.CreateParameter (@strKeyField,200,1,50,strKeyField)
SET RS=Cmd.Execute()
SI CompteRec<1 ALORS
Response.Write (aucun enregistrement)
AUTRE
GetRecord=RS.GetRows(IntPageSize)
Pour i=0 Vers Ubound(GetRecord,2)
Response.Write(GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))'...contenu de sortie
SUIVANT
GetRecord=Nul
FIN SI
SET RS = RIEN
Amis utiles, veuillez le déboguer vous-même. Le total des enregistrements est récupéré à l'aide d'ASP et stocké dans SESSION. Si vous comptez le total des enregistrements à chaque fois, cela prendra beaucoup de temps, bien sûr, si vous souhaitez récupérer le total des enregistrements. procédure stockée, Il est également possible d'enregistrer et de renvoyer le nombre total de pages. Voici le code :
--Obtenir le nombre total d'enregistrements--
SET @tmpSQL='SELECT @getRecordCounts=COUNT('+@strKeyField+') FROM '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts int sortie',@getRecordCounts OUTPUT
--Obtenir le nombre total de pages--
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
SI @getRecordCounts<=@IntPageSize
FIXER @getPageCounts=1
AUTRE
COMMENCER
SI @tempFolatNumber != 0
SET @getPageCounts= (@getRecordCounts/@IntPageSize)+1
AUTRE
SET @getPageCounts= (@getRecordCounts/@IntPageSize)
FIN
N'oubliez pas de renvoyer les paramètres de définition :
@getRecordCounts int sortie,--Renvoyer le total des enregistrements
@getPageCounts int output - renvoie le nombre total de pages