CREAR PROCEDIMIENTO GoalerPageSp
@IntPageSizeint,
@IntCurrPage int,
@strFields nvarchar(2000),
@strTable varchar(200),
@strDonde varchar(800),
@strOrderType varchar(200),
@strKeyField varchar(50)
COMO
ESTABLECER SIN CUENTA EN
DECLARE @tmpSQL nvarchar(4000): declaraciones SQL dinámicas de almacenamiento
DECLARAR @tmpDonde varchar(800)
DECLARAR @tmpAndWhere varchar(800): se utiliza para condiciones de consulta en la página N (>1)
DECLARAR @tmpOrder varchar(200)
DECLARAR @tmpD_X varchar(2)
DECLARAR @tmpMin_MAX varchar(3)
--Establecer condiciones--
SI @strWhere ES NULL O RTRIM(@strWhere)=''
COMENZAR: sin condiciones de consulta
ESTABLECER @tmpDónde=''
ESTABLECER @tmpAndDónde=''
FIN
DEMÁS
COMENZAR: existen condiciones de consulta
ESTABLECER @tmpDónde=' DÓNDE '+@strDónde
ESTABLECER @tmpAndWhere=' AND '+@strWhere
FIN
--Establecer clasificación--
SI @strOrderType != 0
COMENZAR: orden inverso
ESTABLECER @tmpD_X = '<'
ESTABLECER @tmpMin_MAX = 'MIN'
SET @tmpOrder=' ORDEN POR ' +@strKeyField+ ' DESC'
FIN
DEMÁS
COMENZAR
ESTABLECER @tmpD_X = '>'
ESTABLECER @tmpMin_MAX = 'MÁXIMO'
SET @tmpOrder=' ORDEN POR ' +@strKeyField+ ' ASC'
FIN
--consulta SQL--
SI @IntCurrPage=1
Establecer @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder
DEMÁS
SET @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (SELECT '+@tmpMin_MAX+'(' +@strKeyField+') DESDE (SELECCIONAR ARRIBA '+CAST(@IntPageSize*(@IntCurrPage-1) AS VARCHAR)+' '+@strKeyField+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') AS T))'+@tmpAndWhere+' ' +@tmpOrden
EJECUTIVO(@tmpSQL)
IR
Método de llamada:
Tamaño de página interna = 20
strTable= [NombreTabla] 'Nombre de la tabla de datos
strFields= Campo1, Campo2, Campo3, Campo4 'Los nombres de las columnas que se leerán
strKeyField=Campo1'clave principal: Aquí se supone que Campo1 es la clave principal
strDonde='Condición: CampoA='b'
strOrderType=1'Método de clasificación: 1 es orden inverso, 0 es orden secuencial
CurrPage=Solicitud.QueryString(Página)
SI(CurrPage<> Y Isnumeric(CurrPage))ENTONCES
PáginaActual=CLNG(PáginaActual)
IF(Página actual<1)ENTONCES Página actual=1
DEMÁS
Página actual=1
FINALIZAR SI
SI strDonde<> ENTONCES
tmpDonde= DONDE &strDonde
DEMÁS
tmpDonde=
FINALIZAR SI
SI(SESIÓN(RecCount)<>)ENTONCES
SI(SESIÓN(strDonde)<>strDonde)ENTONCES
RecCount=Conn.Execute(SELECT COUNT(&strKeyField&) DESDE &strTable&tmpWhere)(0)
SESIÓN(RecCount)=RecCount
SESIÓN(strDonde)=strDonde
DEMÁS
RecCount=SESIÓN(RecCount)
FINALIZAR SI
DEMÁS
RecCount=Conn.Execute(SELECCIONAR CUENTA(*) DE &strTable&tmpWhere)(0)
SESIÓN(RecCount)=RecCount
SESIÓN(strDonde)=strDonde
FINALIZAR SI
SI(RecCount MOD IntPageSize <>0)ENTONCES
IntPageCount=INT(RecCount/IntPageSize)+1
DEMÁS
IntPageCount=RecCount/IntPageSize
FINALIZAR SI
SET Cmd=Servidor.CrearObjeto(Adodb.Command)
Cmd.CommandType=4
SET Cmd.ActiveConnection=Conexión
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(@strDónde,200,1,800,strDónde)
Cmd.Parameters.Append Cmd.CreateParameter(@strOrderType,4,1,4,strOrderType)
Cmd.Parameters.Append Cmd.CreateParameter(@strKeyField,200,1,50,strKeyField)
ESTABLECER RS=Cmd.Ejecutar()
SI RecCount<1 ENTONCES
Respuesta.Escribir(sin registro)
DEMÁS
GetRecord=RS.GetRows(IntPageSize)
Para i=0 a Ubound(GetRecord,2)
Response.Write(GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))'... contenido de salida
PRÓXIMO
ObtenerRegistro=Nulo
FINALIZAR SI
ESTABLECER RS=NADA
Amigos útiles, depuren usted mismo. Los registros totales se recuperan usando ASP y se almacenan en SESSION. Si cuenta el total de registros cada vez, llevará mucho tiempo, por supuesto, si desea recuperar el total de registros. Procedimiento almacenado. También es posible registrar y devolver el número total de páginas. El siguiente es el código:
--Obtener el número total de registros--
SET @tmpSQL='SELECT @getRecordCounts=COUNT('+@strKeyField+') FROM '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts int salida',@getRecordCounts SALIDA
--Obtener el número total de páginas--
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
SI @getRecordCounts<=@IntPageSize
ESTABLECER @getPageCounts=1
DEMÁS
COMENZAR
SI @tempFolatNumber != 0
ESTABLECER @getPageCounts=(@getRecordCounts/@IntPageSize)+1
DEMÁS
SET @getPageCounts=(@getRecordCounts/@IntPageSize)
FIN
No olvide devolver los parámetros de definición:
@getRecordCounts int salida,--Devuelve registros totales
@getPageCounts int salida: devuelve el número total de páginas