SqlServer萬能分頁儲存過程

2022-07-22 11:27:16 字數 1061 閱讀 4901

alter proc sp_hangkong

@table nvarchar(200), --表,字段,條件,

@field nvarchar(2000)='*', --欄位

@where nvarchar(2000), --條件

@order nvarchar(200), --排序所需要的的

@pagesize int, --每頁顯示的個數

@pagenumber int,--頁數

@total int output

asbegin

declare @strsql nvarchar(4000) --拼接字串

declare @strcount nvarchar(2000) --拼接用於返回的總行數

declare @start int,@end int

set @start = (@pagenumber-1)*@pagesize+1 --開始的頁數

set @end = @start+@pagesize - 1 --結束的頁數

set @strsql = 'select * from (select row_number() over(order by '+@order+') as rowid,'+@field+' from '+@table+' '+@where+') as t where (t.rowid between '+ltrim(str(@start))+' and '+ltrim(str(@end))+')'

set @strcount='select @total=count(*) from '+@table+''

--判斷條件

if(@where is not null or @where != '')

begin

set @strcount=@strcount+' '+@where

end--執行查詢語句

exec(@strsql) --這裡沒有輸出引數

print @strsql

exec sp_executesql @strcount,n'@total int output',@total output

end

萬能儲存過程分頁

引數說明 tblname 需要查詢的表名。如圖書表tblbooks 如果是多表 圖書表關聯圖書分類表 則寫成 tblbooks inner join tblbooktypes on tblbooks.bookid tblbooktypes.bookid fldname 主鍵欄位名 bookid li...

SQL萬能儲存過程分頁

alter proc dbo proc pager tablename varchar 50 表名 refieldsstr varchar 200 欄位名 全部欄位為 orderstring varchar 200 排序字段 必須 wherestring varchar 500 n 條件語句 不用加...

萬能的分頁類

支援任意型別的分頁樣式 to change this template,choose tools templates and open the template in the editor.分頁類 使用方式 page new page page init 1000,20 page setnotact...