转自:http://blog.csdn.net/tem168/article/details/6427123
sqlServer千万数量级分页公共存储过程
通用的数据库分页存储过程:
if exists (select * from dbo.sysobjects where ID = object_ID(N'[dbo].[UP_Pagination]') and OBJECTPROPERTY(ID,N'IsProcedure') = 1)
drop procedure [dbo].[UP_Pagination]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NulLS OFF
GO
CREATE PROCEDURE UP_Pagination
/*
***************************************************************
** 千万数量级分页存储过程 **
***************************************************************
参数说明:
1.tables :表名称,视图,多表查询可用视图做
2.PrimaryKey :主关键字
3.sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc
4.CurrentPage :当前页码
5.PageSize :分页尺寸
6.FIElds :查询字段,默认为 *
6.Filter :过滤语句,不带Where
7.Group :Group语句,不带Group By
***************************************************************/
(
@tables varchar(1000),
@PrimaryKey varchar(100),
@Sort varchar(200) = NulL,
@CurrentPage int = 1,
@PageSize int = 10,
@FIElds varchar(1000) = '*',
@Filter varchar(1000) = NulL,
@Group varchar(1000) = NulL
)
AS
/*默认排序*/
IF @Sort IS NulL OR @Sort = ''
SET @Sort = @PrimaryKey
DECLARE @Sorttable varchar(100)
DECLARE @Sortname varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int
DECLARE @sql nvarchar(1000)
DECLARE @paramdefine nvarchar(100)
DECLARE @TotalRecordCount int
/******************** 数据表中总记录数 ***********************/
SET @sql = 'SELECT @TotalRecordCount = COUNT(*) FROM ' + @tables + ' WHERE 1=1'
IF @Filter IS NOT NulL AND @Filter <> ''
SET @sql = @sql + ' AND ' + @Filter
SET @paramdefine = '@TotalRecordCount INT OUTPUT'
EXEC SP_EXECUTEsql @sql,@paramdefine,@TotalRecordCount OUTPUT
/*************************************************************/
/*设定排序语句.*/ IF CHARINDEX('DESC',@Sort)>0 BEGIN SET @strSortColumn = REPLACE(@Sort,'DESC','') SET @operator = '<=' END ELSE BEGIN IF CHARINDEX('ASC',@Sort) = 0 SET @strSortColumn = REPLACE(@Sort,'ASC','') SET @operator = '>=' END IF CHARINDEX('.',@strSortColumn) > 0 BEGIN SET @Sorttable = SUBSTRING(@strSortColumn,CHARINDEX('.',@strSortColumn)) SET @Sortname = SUBSTRING(@strSortColumn,@strSortColumn) + 1,LEN(@strSortColumn)) END ELSE BEGIN SET @Sorttable = @tables SET @Sortname = @strSortColumn END SELECT @type=t.name,@prec=c.prec FROM sysobjects o JOIN syscolumns c on o.ID=c.ID JOIN systypes t on c.xusertype=t.xusertype WHERE o.name = @Sorttable AND c.name = @Sortname IF CHARINDEX('char',@type) > 0 SET @type = @type + '(' + CAST(@prec AS varchar) + ')' DECLARE @strPageSize varchar(50) DECLARE @strStartRow varchar(50) DECLARE @strFilter varchar(1000) DECLARE @strSimpleFilter varchar(1000) DECLARE @strGroup varchar(1000) /*默认当前页*/ IF @CurrentPage < 1 SET @CurrentPage = 1 /*设置分页参数.*/ SET @strPageSize = CAST(@PageSize AS varchar(50)) SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(50)) /*筛选以及分组语句.*/ IF @Filter IS NOT NulL AND @Filter != '' BEGIN SET @strFilter = ' WHERE ' + @Filter + ' ' SET @strSimpleFilter = ' AND ' + @Filter + ' ' END ELSE BEGIN SET @strSimpleFilter = '' SET @strFilter = '' END IF @Group IS NOT NulL AND @Group != '' SET @strGroup = ' GROUP BY ' + @Group + ' ' ELSE SET @strGroup = '' /*执行查询语句*/ EXEC( ' DECLARE @SortColumn ' + @type + ' SET ROWCOUNT ' + @strStartRow + ' SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + ' SET ROWCOUNT ' + @strPageSize + ' SELECT ' + @FIElds + ' FROM ' + @tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + ' ' ) return @TotalRecordCount GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NulLS ON GO
总结以上是内存溢出为你收集整理的SQLServer千万数量级分页公共存储过程全部内容,希望文章能够帮你解决SQLServer千万数量级分页公共存储过程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)