下面是编程之家 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] GO CREATE 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添加 ) AS SET NOCOUNT ON Declare @SELECT NVARCHAR(1000) ----存放动态生成的sql语句 Declare @strCounts NVARCHAR(1000) ----存放取得查询结果总数的查询语句 Declare @strID NVARCHAR(1000) ----存放取得查询开头或结尾ID的查询语句 Declare @sortTypeA NVARCHAR(10) ----数据排序规则A Declare @SortTypeB NVARCHAR(10) ----数据排序规则B Declare @distSelect NVARCHAR(50) ----对含有disTINCT的查询进行sql构造 Declare @distCounts NVARCHAR(50) ----对含有disTINCT的总数查询进行sql构造 DECLARE @SortfieldA NVARCHAR(50) ----对含有是否还有排序字段时的排序方式组合A DECLARE @SortfieldB NVARCHAR(50) ----对含有是否还有排序字段时的排序方式组合B IF @distinct = 0 BEGIN SET @distSelect = 'SELECT ' SET @distCounts = ' COUNT(*)' END ELSE BEGIN SET @distSelect = 'SELECT distinct ' SET @distCounts = ' COUNT(disTINCT '[email protected]+')' END IF @sort=0 BEGIN SET @SortTypeB=' ASC ' SET @sortTypeA=' DESC ' END ELSE BEGIN SET @SortTypeB=' DESC ' SET @sortTypeA=' ASC ' END IF @fieldSort IS NOT NULL AND @fieldSort<>'' --排序字段不为空时 BEGIN SET @SortfieldB=' order by '+ @fieldSort +' '+ @SortTypeB SET @SortfieldA=' order by '+ @fieldSort +' '+ @SortTypeA END ELSE 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 END ELSE 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 out DECLARE @tmpCounts INT IF @counts = 0 SET @tmpCounts = 1 ELSE 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 @strCounts SET NOCOUNT OFF
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。