BUAT PROSEDUR GoalerPageSp
@IntPageSize ke dalam,
@IntCurrPage ke dalam,
@strFields nvarchar(2000),
@strTable varchar(200),
@strDi mana varchar(800),
@strOrderType varchar(200),
@strKeyField varchar(50)
SEBAGAI
SETEL NOCOUNT AKTIF
DECLARE @tmpSQL nvarchar(4000)--Penyimpanan pernyataan SQL dinamis
DEKLARASIKAN @tmpWhere varchar(800)
DECLARE @tmpAndWhere varchar(800)--digunakan untuk kondisi kueri di halaman N (>1)
DEKLARASIKAN @tmpOrder varchar(200)
DEKLARASIKAN @tmpD_X varchar(2)
DEKLARASIKAN @tmpMin_MAX varchar(3)
--Atur kondisi--
JIKA @strWhere IS NULL ATAU RTRIM(@strWhere)=''
BEGIN--Tidak ada ketentuan kueri
SETEL @tmpWhere=''
SETEL @tmpAndWhere=''
AKHIR
KALAU TIDAK
BEGIN--Ada kondisi kueri
SET @tmpWhere=' WHERE '+@strWhere
SET @tmpAndWhere=' DAN '+@strWhere
AKHIR
--Atur penyortiran--
JIKA @strOrderType != 0
MULAI--urutan terbalik
SETEL @tmpD_X = '<'
SET @tmpMin_MAX = 'MIN'
SET @tmpOrder=' PESANAN OLEH ' +@strKeyField+ ' DESC'
AKHIR
KALAU TIDAK
MULAI
SETEL @tmpD_X = '>'
SET @tmpMin_MAX = 'MAX'
SET @tmpOrder=' PESANAN OLEH ' +@strKeyField+ ' ASC'
AKHIR
--Permintaan SQL--
JIKA @IntCurrPage=1
Setel @tmpSQL='PILIH ATAS '+CAST(@IntPageSize SEBAGAI VARCHAR)+' '+@strFields+' DARI '+@strTable+' '+@tmpWhere+' '+@tmpOrder
KALAU TIDAK
SET @tmpSQL='PILIH ATAS '+CAST(@IntPageSize SEBAGAI VARCHAR)+' '+@strFields+' DARI '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (PILIH '+@tmpMin_MAX+'(' +@strKeyField+') DARI (PILIH ATAS '+CAST(@IntPageSize*(@IntCurrPage-1) SEBAGAI VARCHAR)+' '+@strKeyField+' DARI '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') SEBAGAI T))'+@tmpAndWhere+' ' +@tmpPesanan
EXEC(@tmpSQL)
PERGI
Metode panggilan:
Ukuran Halaman Int=20
strTable= [NamaTabel] 'Nama tabel data
strFields= Field1,Field2,Field3,Field4 'Nama kolom yang akan dibaca
strKeyField=Field1'kunci utama: Di sini diasumsikan bahwa Field1 adalah kunci utama
strWhere='Kondisi: BidangA='b'
strOrderType=1'Metode pengurutan: 1 adalah urutan terbalik, 0 adalah urutan berurutan
CurrPage=Permintaan.QueryString(Halaman)
IF(CurrPage<> Dan Isnumerik(CurrPage))THEN
Halaman Saat Ini=CLNG(Halaman Saat Ini)
JIKA(Halaman Saat Ini<1) MAKA Halaman Saat Ini=1
KALAU TIDAK
Halaman Saat Ini=1
AKHIR JIKA
JIKA strDi mana<> LALU
tmpWhere= DIMANA &strDimana
KALAU TIDAK
tmpDi mana=
AKHIR JIKA
IF(SESSION(RecCount)<>)THEN
IF(SESI(strWhere)<>strWhere)THEN
RecCount=Samb.Jalankan(PILIH JUMLAH(&strKeyField&) DARI &strTable&tmpWhere)(0)
SESSION(RecCount)=RecCount
SESI(strWhere)=strWhere
KALAU TIDAK
Penghitungan Ulang=SESI(Penghitungan Ulang)
AKHIR JIKA
KALAU TIDAK
RecCount=Samb.Jalankan(PILIH JUMLAH(*) DARI &strTable&tmpWhere)(0)
SESSION(RecCount)=RecCount
SESI(strWhere)=strWhere
AKHIR JIKA
JIKA(RecCount MOD IntPageSize <>0) LALU
IntPageCount=INT(RecCount/IntPageSize)+1
KALAU TIDAK
IntPageCount=RecCount/IntPageSize
AKHIR JIKA
SET Cmd=Server.CreateObject(Adodb.Command)
Cmd.Tipe Perintah=4
SET Cmd.ActiveConnection=Samb
Cmd.CommandText=GoalerPageSp
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@IntPageSize,4,1,4,IntPageSize)
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@IntCurrPage,4,1,4,CurrPage)
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@strFields,200,1,2000,strFields)
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@strTable,200,1,200,strTable)
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@strWhere,200,1,800,strWhere)
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@strOrderType,4,1,4,strOrderType)
Cmd.Parameters.Tambahkan Cmd.CreateParameter(@strKeyField,200,1,50,strKeyField)
SET RS=Cmd.Jalankan()
JIKA Hitung Ulang<1 MAKA
Respon. Tulis (tidak ada catatan)
KALAU TIDAK
GetRecord=RS.GetRows(IntPageSize)
Untuk i=0 Ke Ubound(GetRecord,2)
Response.Write(GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))'...konten keluaran
BERIKUTNYA
DapatkanRecord=Nol
AKHIR JIKA
SETEL RS=TIDAK ADA
Teman-teman yang membantu, silakan debug sendiri. Total record diambil menggunakan ASP dan disimpan di SESSION. Jika Anda menghitung total record setiap saat, itu akan sangat memakan waktu prosedur tersimpan, Dimungkinkan juga untuk mencatat dan mengembalikan jumlah halaman. Berikut ini adalah kodenya:
--Dapatkan jumlah total catatan--
SET @tmpSQL='PILIH @getRecordCounts=COUNT('+@strKeyField+') DARI '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts int keluaran',@getRecordCounts OUTPUT
--Dapatkan jumlah total halaman--
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
JIKA @getRecordCounts<=@IntPageSize
SETEL@getPageCounts=1
KALAU TIDAK
MULAI
JIKA @tempFolatNumber != 0
SET @getPageCounts=(@getRecordCounts/@IntPageSize)+1
KALAU TIDAK
SET @getPageCounts=(@getRecordCounts/@IntPageSize)
AKHIR
Jangan lupa untuk mengembalikan parameter definisi:
@getRecordCounts int output,--Mengembalikan total catatan
@getPageCounts int output--mengembalikan jumlah total halaman