一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net高效的分页方法超大数据量大并且带查询参数

时间:2010-05-27 编辑:简简单单 来源:一聚教程网

asp教程.net高效的分页方法超大数据量大并且带查询参数

create   Proc [dbo].[GetRS]
@QueryStr nvarchar(300),--表名、视图名、查询语句
@PageSize int=10,--每页的大小(行数)
@PageCurrent int=1,--要显示的页
@FdShow nvarchar (100)='',--要显示的字段列表,如果查询结果有标识字段,需要指定此值,且不包含标识字段
@FdOrder nvarchar (100)='',--排序字段列表
@WhereStr nvarchar (200)='',     --内容是' id=3 and model_no like '%24%' and '
@RSCount int=0 output
as
set @FdShow=' '+@FdShow+' '
set @FdOrder= ' '+@FdOrder+' '
set @WhereStr= ' '+@WhereStr+' '

declare @FdName nvarchar(250)--表中的主键或表、临时表中的标识列名
,@Id1 varchar(20),@Id2 varchar(20)--开始和结束的记录号
,@Obj_ID int --对象ID
,@Temp nvarchar(300) --临时语句
,@strParam nvarchar(100) --临时参数

declare @strfd nvarchar(2000)--复合主键列表
,@strjoin nvarchar(4000)--连接字段
,@strwhere nvarchar(2000)--查询条件
--检查输入参数
set @QueryStr=ltrim(rtrim(@QueryStr))
select @Obj_ID=object_id(@QueryStr)
,@FdShow=case isnull(@FdShow,'') when '' then ' *' else ' '+@FdShow end
,@FdOrder=case isnull(@FdOrder,'') when '' then '' else ' order by '+@FdOrder end
,@QueryStr=case when @Obj_ID is not null then ' '+@QueryStr else ' ('+@QueryStr+') a' end
--输出总记录数
SET @Temp= 'select @RSCount=count(*)  FROM ' + @QueryStr+' '+@WhereStr
SET @strParam = N'@RSCount INT OUT'
EXECUTE sp_executeSQL  @Temp,@strParam,@RSCount out
--如果显示第一页,可以直接用top来完成
if @PageCurrent=1
begin
select @Id1=cast(@PageSize as varchar(20))
exec('select top '+@Id1+@FdShow+' from '+@QueryStr+@WhereStr+@FdOrder)
return
end
--如果是表,则检查表中是否有标识更或主键
if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=1
begin
select @Id1=cast(@PageSize as varchar(20))
,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20))
select @FdName=name from syscolumns where id=@Obj_ID and status=0x80
if @@rowcount=0--如果表中无标识列,则检查表中是否有主键
begin
if not exists(select 1 from sysobjects where parent_obj=@Obj_ID and xtype='PK')
goto lbusetemp--如果表中无主键,则用临时表处理
select @FdName=name from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))

if @@rowcount>1--检查表中的主键是否为复合主键
begin
select @strfd='',@strjoin='',@strwhere=''
select @strfd=@strfd+',['+name+']'
,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'
,@strwhere=@strwhere+' and b.['+name+'] is null'
from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
select @strfd=substring(@strfd,2,2000)
,@strjoin=substring(@strjoin,5,4000)
,@strwhere=substring(@strwhere,5,4000)
goto lbusepk
end
end
end
else
goto lbusetemp
/*--使用标识列或主键为单一字段的处理方法--*/
lbuseidentity:
        if len(@WhereStr)>10
begin
exec('select top '+@Id1+@FdShow+' from '+@QueryStr
+@WhereStr+' and '+@FdName+' not in(select top '
+@Id2+' '+@FdName+' from '+@QueryStr+@WhereStr+@FdOrder
+')'+@FdOrder
)
return
end
else
begin
exec('select top '+@Id1+@FdShow+' from '+@QueryStr
+' where '+@FdName+' not in(select top '
+@Id2+' '+@FdName+' from '+@QueryStr+@FdOrder
+')'+@FdOrder
)
return
end
/*--表中有复合主键的处理方法--*/
lbusepk:
exec('select '+@FdShow+' from(select top '+@Id1+' a.* from
(select top 100 percent * from '+@QueryStr+@FdOrder+') a
left join (select top '+@Id2+' '+@strfd+'
from '+@QueryStr+@FdOrder+') b on '+@strjoin+'
where '+@strwhere+') a'
)
return
/*--用临时表处理的方法--*/
lbusetemp:
select @FdName='[ID_'+cast(newid() as varchar(40))+']'
,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20))
,@Id2=cast(@PageSize*@PageCurrent-1 as varchar(20))
exec('select '+@FdName+'=identity(int,0,1),'+@FdShow+'
into #tb from'+@QueryStr+@FdOrder+'
select '+@FdShow+' from #tb where '+@FdName+' between '
+@Id1+' and '+@Id2
)

'分析二

ALTER PROCEDURE dbo.proc_ListPage
(
@tblName nvarchar(200), ----要显示的表或多个表的连接
@fldName nvarchar(500) = '*', ----要显示的字段列表
@pageSize int = 1, ----每页显示的记录个数
@page int = 10, ----要显示那一页的记录
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output, ----查询到的记录数
@fldSort nvarchar(200) = null, ----排序字段列表或条件
@Sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ----查询条件,不需where
@ID nvarchar(150), ----主表的主键
@Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句

Declare @strSortType nvarchar(10) ----数据排序规则A
Declare @strFSortType nvarchar(10) ----数据排序规则B

Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造


if @Dist = 0
begin
  set @SqlSelect = 'select '
  set @SqlCounts = 'Count(*)'
end
else
begin
  set @SqlSelect = 'select distinct '
  set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end


if @Sort=0
begin
  set @strFSortType=' ASC '
  set @strSortType=' DESC '
end
else
begin
  set @strFSortType=' DESC '
  set @strSortType=' ASC '
end

 

--------生成查询语句--------
--此处@strTmp为取得查询结果数量的语句
if @strCondition is null or @strCondition='' --没有设置显示条件
begin
  set @sqlTmp = @fldName + ' From ' + @tblName
  set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
  set @strID = ' From ' + @tblName
end
else
begin
  set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
  set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
  set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end

----取得查询结果总数量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
  set @tmpCounts = 1
else
  set @tmpCounts = @Counts

  --取得分页总数
  set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize

  /**//**当前页大于总页数 取最后一页**/
  if @page>@pageCount
  set @page=@pageCount

  --/*-----数据分页2分处理-------*/
  declare @pageIndex int --总数/页大小
  declare @lastcount int --总数%页大小 

  set @pageIndex = @tmpCounts/@pageSize
  set @lastcount = @tmpCounts%@pageSize
  if @lastcount > 0
  set @pageIndex = @pageIndex + 1
  else
  set @lastcount = @pagesize

  --//***显示分页
  if @strCondition is null or @strCondition='' --没有设置显示条件
  begin
  if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
  begin 
  set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
  +' order by '+ @fldSort +' '+ @strFSortType+')'
  +' order by '+ @fldSort +' '+ @strFSortType 
  end
  else
  begin
  set @page = @pageIndex-@page+1 --后半部分数据处理
  if @page <= 1 --最后一页数据显示
  set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
  else  
  set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
  +' order by '+ @fldSort +' '+ @strSortType+')'

  +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
  end
  end

  else --有查询条件
  begin
  if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
  begin 
  set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from '+@tblName
  +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
  +' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'
  +' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType  
  end
  else
  begin 
  set @page = @pageIndex-@page+1 --后半部分数据处理
  if @page <= 1 --最后一页数据显示
  set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  +' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
  else
  set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
  +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
  +' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'
  + @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
  end  
  end

------返回查询结果-----
exec sp_executesql @strTmp
--print @strTmp
SET NOCOUNT OFF

热门栏目