create proc sp_publicturnpage(
@tbname nvarchar(2000)='',--表名,如 pinyin
@pagesizeint=10,--每頁的記錄數,預設為 10
@curpageint=1,--表示當前頁 1
@keyfieldnvarchar(100)='id',--關鍵欄位名,預設為 id,該欄位要求是表中的索引 或 無重複和不為空的字段
@keyascdescnvarchar(4)='asc',--關鍵字的公升、降序,預設為公升序 asc , 降序為 desc
@fieldsnvarchar(2000)='*',--所選擇的列名,預設為全選
@conditionnvarchar(2000)='',--where 條件,預設為空
@ordernvarchar(200)=''--排序條件,預設為空
)asif @tbname = ''
begin
raiserror('請指定表名!',11,1)
return
endif @pagesize <=0 or @curpage <0
begin
raiserror('當前頁數和每頁的記錄數都必須大於零!',11,1)
return
endif @keyascdesc = 'desc'
set @keyascdesc = '<'
else
set @keyascdesc = '>'
if @condition <> ''
set @condition = ' where ' + @condition
declare @sql nvarchar(2000)
set @sql = ''
if @curpage = 1
set @sql = @sql + 'select top ' + cast(@pagesize as nvarchar(20)) + ' ' + @fields + ' from ' + @tbname + @condition + ' ' + @order
else
begin
declare @itopnum int
set @itopnum = @pagesize * (@curpage - 1)
set @sql = @sql + 'declare @slastvalue nvarchar(100)' + char(13)
set @sql = @sql + 'select top ' + cast(@itopnum as nvarchar(20)) + ' @slastvalue=' + @keyfield + ' from ' + @tbname + @condition + ' ' + @order + char(13)
declare @condition2 nvarchar(200)
if @condition = ''
set @condition2 = ' where ' + @keyfield + @keyascdesc + '@slastvalue '
else
set @condition2 = ' and ' + @keyfield + @keyascdesc + '@slastvalue '
set @sql = @sql + 'select top ' + cast(@pagesize as nvarchar(20)) + ' ' + @fields + ' from ' + @tbname + @condition + @condition2 + @order
endexecute sp_executesql @sql
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
資料海量分頁儲存過程(3)
declare strsql varchar 6000 主語句 declare strtmp varchar 1000 臨時變數 declare strorder varchar 500 排序型別 if ordertype 0 begin set strtmp select min set stro...
MSSQL 儲存過程分頁 測試 3
declare user condition varchar 500 declare user sort varchar 20 declare user rank varchar 4 declare page size real declare page order int declare sqlq...