alter procedure consignment
@tablename varchar(80) ,
@strorder varchar(50) ,
@pageindex int = 1,
@pagesize int = 15,
@strgetfields varchar(200) = '*',
@output int output
as
begin
declare @strsql varchar(500)
declare @sql nvarchar(1000)
declare @r bigint
set @sql= n'select @r=count(*) from '+@tablename
exec sp_executesql @sql, n' @r bigint output', @r output
set @output= @r
if(@pageindex =1)
begin
set @strsql='select top '+str(@pagesize)+' '+@strgetfields+' from '+@tablename+' order by '+@strorder
end
else
set @strsql='select top '+str(@pagesize)+' '+@strgetfields+' from '+@tablename+' where ('+@strorder
+' >= ( select max('+@strorder+') from ( select top '+str(@pagesize*@pageindex)+' * from '+@tablename+' order by '
+@strorder+' ) as temptable)) order by '+ @strorder
select @strsql
exec(@strsql)
end
alter procedure allproce@tablename varchar(200) , --表名
@strgetfields varchar(200) = '*', --查詢列名
@pageindex int = 1 , --頁碼
@pagesize int = 15, --頁面大小
@strwhere varchar(100) = '', --查詢條件
@strorder varchar(100) = '', --排序列名
@intorder bit = 0, --排序型別 1為公升序
@countall bigint output --返回紀錄總數用於計算頁面數
as
begin
declare @strsql varchar(500) --主語句
declare @strtemp varchar(100) --臨時變數
declare @strorders varchar(50) --排序語句
declare @table varchar(70)
declare @sql nvarchar(1000)
declare @r bigint
set @sql= n'select @r=count(*) from '+convert(nvarchar(200),@tablename)
exec sp_executesql @sql, n' @r bigint output', @r output
set @countall= @r
if @intorder = 0
begin
--為0是公升序
set @strtemp = '>(select max'
set @strorders = ' order by '+@strorder+' asc '
end
else
begin
--否則為降序
set @strtemp = '<(select min'
set @strorders = ' order by '+@strorder+' desc '
end
if @pageindex =1 --第一頁直接讀出紀錄
begin
if @strwhere = ''
begin
set @strsql = 'select top '+str(@pagesize)+' '+@strgetfields+' from '+@tablename+' '+@strorders
end
else
begin
set @strsql = 'select top '+str(@pagesize)+' '+@strgetfields+ ' from '+@tablename+' where '+@strwhere+' '+@strorders
end
end
else
begin
set @strsql = 'select top'+str(@pagesize)+' '+@strgetfields+' from '+@tablename+' where '+@strorder+' '+@strtemp+' ('+@strorder+')'
+' from (select top '+str((@pageindex-1)*@pagesize)+' '+@strgetfields+' from '+@tablename+ ' '+@strorders+ ') as temptable ) '+@strorders
if @strwhere != ' '
begin
set @strsql = 'select top '+str(@pagesize)+ ' '+@strgetfields+' from '+@tablename+ ' where '+@strorder+ ' '+@strtemp+' ('+@strorder+') '
+' from(select top '+str((@pageindex-1)*@pagesize)+' '+@strgetfields+' from '+@tablename+' where '+@strwhere+' ' +@strorders+') as temptable) where '+@strwhere+' '+@strorders
end
end
exec(@strsql)
end
go
sql 儲存過程分頁
create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...
SQL 儲存過程 分頁
1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...
SQL 分頁儲存過程
create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...