プロシージャの作成 GoalerPageSp
@IntPageSize int、
@IntCurrPage int、
@strFields nvarchar(2000)、
@strTable varchar(200)、
@strWhere varchar(800)、
@strOrderType varchar(200)、
@strKeyField varchar(50)
として
ノーカウントをオンに設定
DECLARE @tmpSQL nvarchar(4000) -- ストレージ動的 SQL ステートメント
@tmpWhere varchar(800) を宣言します
DECLARE @tmpAndWhere varchar(800) -- ページ N (>1) のクエリ条件に使用されます
@tmpOrder varchar(200) を宣言します
@tmpD_X varchar(2) を宣言します
@tmpMin_MAX varchar(3) を宣言します
--条件を設定します--
@strWhere が NULL または RTRIM(@strWhere)='' の場合
BEGIN -- クエリ条件なし
SET @tmpWhere=''
SET @tmpAndWhere=''
終わり
それ以外
BEGIN -- クエリ条件があります
SET @tmpWhere=' WHERE '+@strWhere
SET @tmpAndWhere=' AND '+@strWhere
終わり
--並べ替えの設定--
IF @strOrderType != 0
BEGIN -- 逆の順序
SET @tmpD_X = '<'
SET @tmpMin_MAX = 'MIN'
SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' DESC'
終わり
それ以外
始める
SET @tmpD_X = '>'
SET @tmpMin_MAX = 'MAX'
SET @tmpOrder=' ORDER BY ' +@strKeyField+ ' ASC'
終わり
--SQLクエリ--
IF @IntCurrPage=1
Set @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder
それ以外
SET @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' FROM '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (SELECT '+@tmpMin_MAX+'(' +@strKeyField+') FROM (先頭を選択) '+CAST(@IntPageSize*(@IntCurrPage-1) AS VARCHAR)+' '+@strKeyField+' FROM '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') AS T))'+@tmpAndWhere+' ' +@tmpOrder
EXEC(@tmpSQL)
行く
呼び出し方法:
IntPageSize=20
strTable= [TableName] 'データテーブル名
strFields= Field1,Field2,Field3,Field4 '読み込む列名
strKeyField=Field1'主キー: ここでは、Field1 が主キーであると仮定します。
strWhere='条件: フィールドA='b'
strOrderType=1'ソート方法: 1 は逆順、0 は順序
CurrPage=Request.QueryString(ページ)
IF(CurrPage<> And Isnumeric(CurrPage))THEN
現在のページ=CLNG(現在のページ)
IF(CurrPage<1)THEN CurrPage=1
それ以外
現在のページ=1
終了 IF
IF strWhere<> THEN
tmpWhere= WHERE &strWhere
それ以外
tmpどこ=
終了 IF
IF(SESSION(RecCount)<>)THEN
IF(SESSION(strWhere)<>strWhere)THEN
RecCount=Conn.Execute(SELECT COUNT(&strKeyField&) FROM &strTable&tmpWhere)(0)
SESSION(RecCount)=RecCount
SESSION(strWhere)=strWhere
それ以外
RecCount=SESSION(RecCount)
終了 IF
それ以外
RecCount=Conn.Execute(SELECT COUNT(*) FROM &strTable&tmpWhere)(0)
SESSION(RecCount)=RecCount
SESSION(strWhere)=strWhere
終了 IF
IF(RecCount MOD IntPageSize <>0)THEN
IntPageCount=INT(RecCount/IntPageSize)+1
それ以外
IntPageCount=RecCount/IntPageSize
終了 IF
SET Cmd=Server.CreateObject(Adodb.Command)
Cmd.CommandType=4
SET Cmd.ActiveConnection=接続
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()
IF RecCount<1 THEN
応答.書き込み(記録なし)
それ以外
GetRecord=RS.GetRows(IntPageSize)
i=0 の場合、Ubound(GetRecord,2) へ
Response.Write(GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))'...出力内容
次
GetRecord=Null
終了 IF
RS=何も設定しない
親切な友人、それを自分でデバッグしてください。合計レコードは ASP を使用して取得され、SESSION に保存されます。もちろん、合計レコードを取得する場合は非常に時間がかかります。ストアド プロシージャ。合計ページ数を記録して返すこともできます。コードは次のとおりです。
--レコードの総数を取得します--
SET @tmpSQL='SELECT @getRecordCounts=COUNT('+@strKeyField+') FROM '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts int 出力',@getRecordCounts OUTPUT
--総ページ数を取得--
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
IF @getRecordCounts<=@IntPageSize
@getPageCounts=1 を設定します
それ以外
始める
IF @tempFolatNumber != 0
SET @getPageCounts=(@getRecordCounts/@IntPageSize)+1
それ以外
SET @getPageCounts=(@getRecordCounts/@IntPageSize)
終わり
定義パラメータを返すことを忘れないでください。
@getRecordCounts int 出力、--合計レコードを返します
@getPageCounts int 出力 -- 総ページ数を返します。