drop proc [dbo].[up_getrecordbypage]
gocreate procedure [dbo].[up_getrecordbypage]
@tblname varchar(255), -- 表名
@fldname varchar(255), -- 主鍵欄位名
@strcontent nvarchar(255), -- 查詢結果
@pagesize int , -- 頁尺寸
@pageindex int, -- 頁碼
@order_by nvarchar(2000), -- 排序字串
@strwhere varchar(1000) , -- 查詢條件 (注意: 不要加 where)
@totalrecords int output -- 返回記錄總數
asdeclare @alls int
set nocount on
declare @where_string nvarchar(1000)
declare @sql nvarchar(1200)
declare @pagecount int
declare @row_num int
if @strwhere=''
begin
set @where_string=@strwhere
end
else
begin
set @where_string=' where '+@strwhere
set @strwhere=' and '+@strwhere
end
if not @order_by=''
begin
set @order_by=' order by '+@order_by
end
create table #pgs(id [nvarchar] (1),pgs [int])
--delete from pgs
begin
set @sql=n'insert #pgs(pgs,id) select count(*) as pgs,''1'' as id from '+@tblname+@where_string
exec sp_executesql @sql
print @sql
select @row_num=pgs from #pgs where id='1'
select @totalrecords=@row_num
--select @totalrecords=@spintrootrecordcount
if @row_num>@pagesize
begin
set @row_num=@pageindex*@pagesize
if @row_num=@pagesize
begin
set @sql=n'select top '+cast(@pagesize as nvarchar(100))+@strcontent+' from '+@tblname+@where_string+@order_by
print @sql
--exec sp_executesql @sql
end
else
begin
set @row_num=(@pageindex-1)*@pagesize
set @pagecount=@row_num
set @sql=n'select top '+cast(@pagesize as nvarchar(100))+@strcontent+'
from '+@tblname+' where '+@fldname+' not in (select top '+cast(@pagecount as nvarchar(100))+@fldname+' from '+@tblname+@where_string+@order_by+') '+@strwhere+@order_by
print @sql
exec sp_executesql @sql
end
endelse
set @sql='select '+@strcontent+' from '+@tblname+@where_string+@order_by
exec sp_executesql @sql
enddrop proc turnpage
gocreate proc turnpage
@qcols varchar(200), --需要查詢的列
@qtables varchar(200), --需要查詢的表和條件
@ikey varchar (20), --標識字段
@okey varchar(20), --排序字段
@pagesize int, --每頁的行數
@pagenumber int --要顯示的頁碼, 從開始
asset nocount on
begin
declare @sqltext as varchar(1000)
declare @sqltable as varchar(1000)
set @sqltable = 'select top ' + cast((@pagenumber + 1) * @pagesize as varchar(30)) + ' ' + @qcols +' from '+ @qtables + ' order by '+@okey+ ''
set @sqltext =
'select top ' + cast(@pagesize as varchar(30)) + ' * ' +
'from (' + @sqltable + ') as tablea ' +
'where ' + @ikey + ' not in(select top ' +
cast(@pagenumber * @pagesize as varchar(30)) + ' ' + @ikey +
' from (' + @sqltable + ') as tableb)'
exec (@sqltext)
--print(@sqltext)
endgo
教你快速掌握兩個分頁儲存過程的用法
兩個分頁儲存過程的用法 basic pagination2005 只能在sqlserver2005下用 basic pagination2000 可在sqlserver2000和sqlserver2005下通用 兩個儲存過程的引數是一樣的,其中的引數說明在 中已有注釋。需要注意的是當 isrecou...
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
找了個分頁儲存過程
create procedure usp paginglarge tablenames varchar 200 表名,可以是多個表,但不能用別名 primarykey varchar 100 主鍵,可以為空,但 order為空時該值不能為空 fields varchar 200 要取出的字段,可以是...