下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
IF exists (SELECT * FROM dbo.sysobjects WHERE ID = object_ID(N'[dbo].[proc_DataPagingList]') and OBJECTPROPERTY(ID,N'IsProcedure') = 1)drop procedure [dbo].[proc_DataPagingList]GOCREATE PROCEDURE [dbo].[proc_DataPagingList](@tablename NVARCHAR(200),----要显示的表或多个表的连接@fIEldnames NVARCHAR(200)='*',----要显示的字段列表@pageSize INT = 10,----每页显示的记录个数@page INT = 10,----要显示那一页的记录@pageCount INT = 1 output,----查询结果分页后的总页数@counts INT = 1 output,----查询到的总记录数@fIEldSort NVARCHAR(200)= null,----排序字段列表或条件@sort BIT = 1,----排序方法,0为升序,1为降序--程序传参如:' SortA Asc,SortB Desc,SortC ')@condition NVARCHAR(200)= null,----查询条件,不需WHERE@keyID NVARCHAR(100),----主表的主键@distinct BIT = 0 ----是否添加查询字段的 disTINCT 默认0不添加/1添加)ASSET NOCOUNT ONDeclare @SELECT NVARCHAR(1000) ----存放动态生成的SQL语句Declare @strCounts NVARCHAR(1000) ----存放取得查询结果总数的查询语句Declare @strID NVARCHAR(1000) ----存放取得查询开头或结尾ID的查询语句Declare @sortTypeA NVARCHAR(10) ----数据排序规则ADeclare @SortTypeB NVARCHAR(10) ----数据排序规则BDeclare @distSelect NVARCHAR(50) ----对含有disTINCT的查询进行sql构造Declare @distCounts NVARCHAR(50) ----对含有disTINCT的总数查询进行sql构造DECLARE @SortfIEldA NVARCHAR(50) ----对含有是否还有排序字段时的排序方式组合ADECLARE @SortfIEldB NVARCHAR(50) ----对含有是否还有排序字段时的排序方式组合BIF @distinct = 0 BEGIN SET @distSelect = 'SELECT ' SET @distCounts = ' COUNT(*)' ENDELSE BEGIN SET @distSelect = 'SELECT distinct ' SET @distCounts = ' COUNT(disTINCT '[email protected]+')' ENDIF @sort=0 BEGIN SET @SortTypeB=' ASC ' SET @sortTypeA=' DESC ' ENDELSE BEGIN SET @SortTypeB=' DESC ' SET @sortTypeA=' ASC ' ENDIF @fIEldSort IS NOT NulL AND @fIEldSort<>'' --排序字段不为空时 BEGIN SET @SortfIEldB=' order by '+ @fIEldSort +' '+ @SortTypeB SET @SortfIEldA=' order by '+ @fIEldSort +' '+ @SortTypeA ENDELSE BEGIN SET @SortfIEldB='' SET @SortfIEldA='' END--------生成查询语句----------此处@strCounts为取得查询结果数量的语句IF @condition is null or @condition='' --没有设置显示条件 BEGIN SET @SELECT = @fIEldnames + ' FROM ' + @tablename SET @strCounts = @distSelect+' @counts='[email protected]+' FROM '[email protected] SET @strID = ' FROM ' + @tablename ENDELSE BEGIN SET @SELECT = + @fIEldnames + 'FROM ' + @tablename + ' WHERE ' + @condition SET @strCounts = @distSelect+' @counts='[email protected]+' FROM '[email protected] + ' WHERE ' + @condition SET @strID = ' FROM ' + @tablename + ' WHERE ' + @condition END----取得查询结果总数量-----exec sp_executesql @strCounts,N'@counts INT out ',@counts outDECLARE @tmpCounts INTIF @counts = 0 SET @tmpCounts = 1ELSE SET @tmpCounts = @counts --取得分页总数 SET @pageCount=(@tmpCount[email protected])/@pageSize --/**当前页大于总页数 取最后一页**/ IF @page>@pageCount SET @[email protected] --/*-----数据分页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 @condition is null or @condition='' --没有设置显示条件 BEGIN IF @pageIndex<2 or @page<[email protected] / 2 + @pageIndex % 2 --前半部分数据处理 BEGIN SET @[email protected]+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fIEldnames+' FROM '[email protected] + ' WHERE '[email protected]+' not in('+ @distSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @keyID +' FROM '[email protected] + @SortfIEldB +')' + @SortfIEldB END ELSE BEGIN SET @page = @[email protected]+1 --后半部分数据处理 IF @page <= 1 --最后一页数据显示 SET @[email protected]+' * FROM ('[email protected]+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fIEldnames+' FROM '[email protected] + @SortfIEldA+') AS TempTB '[email protected] ELSE SET @[email protected]+' * FROM ('[email protected]+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fIEldnames+' FROM '[email protected] + ' WHERE '[email protected]+' not in('+ @distSelect+' top '+ CAST(@pageSize*(@page-2)[email protected] as Varchar(20)) +' '+ @keyID +' FROM '[email protected] + @SortfIEldA+')' + @SortfIEldA+') AS TempTB '[email protected] END END ELSE --有查询条件 BEGIN IF @pageIndex<2 or @page<[email protected] / 2 + @pageIndex % 2 --前半部分数据处理 BEGIN SET @[email protected]+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fIEldnames +' FROM '[email protected] + ' WHERE '[email protected]+' not in('+ @distSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @keyID +' FROM '[email protected] + ' Where ' + @condition + @SortfIEldB+')'+' AND ' + @condition + @SortfIEldB END ELSE BEGIN SET @page = @[email protected]+1 --后半部分数据处理 IF @page <= 1 --最后一页数据显示 SET @[email protected]+' * FROM ('[email protected]+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fIEldnames+' FROM '[email protected] + ' WHERE '+ @condition [email protected]+') AS TempTB '[email protected] ELSE SET @[email protected]+' * FROM ('[email protected]+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fIEldnames+' FROM '[email protected] + ' WHERE '[email protected]+' not in('+ @distSelect+' top '+ CAST(@pageSize*(@page-2)[email protected] as Varchar(20)) +' ' + @keyID +' FROM '[email protected] +' WHERE '+ @condition [email protected]+')' +' AND '+ @condition [email protected]+') AS TempTB ' + @SortfIEldB END END------返回查询结果-----exec sp_executesql @strCountsSET NOCOUNT OFF
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的SQL Server 2005高效分页存储过程全部内容,希望文章能够帮你解决SQL Server 2005高效分页存储过程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)