CREATEPROCEDURECN5135_SP_Pagination
/*
***************************************************************
**中国无忧商务网千万数量级分页存储过程**
***************************************************************
参数说明:
1.Tables:表名称,视图
2.PrimaryKey:主关键字
3.Sort:排序语句,不带OrderBy比如:NewsIDDesc,OrderRowsAsc
4.CurrentPage:当前页码
5.PageSize:分页尺寸
6.Filter:过滤语句,不带Where
7.Group:Group语句,不带GroupBy
版权所有:中国无忧商务网(http://www.cn5135.com)
欢迎试用,有什么心得请Mail:xzhijun@GMail.com
注:转载请保留相关版权说明,谢谢!^_^
***************************************************************/
(
@Tablesvarchar(1000),
@PrimaryKeyvarchar(100),
@Sortvarchar(200)=NULL,
@CurrentPageint=1,
@PageSizeint=10,
@Fieldsvarchar(1000)='*',
@Filtervarchar(1000)=NULL,
@Groupvarchar(1000)=NULL
)
AS
/*默认排序*/
IF@SortISNULLOR@Sort=''
SET@Sort=@PrimaryKey
DECLARE@SortTablevarchar(100)
DECLARE@SortNamevarchar(100)
DECLARE@strSortColumnvarchar(200)
DECLARE@operatorchar(2)
DECLARE@typevarchar(100)
DECLARE@precint
/*设定排序语句.*/
IFCHARINDEX('DESC',@Sort)>0
BEGIN
SET@strSortColumn=REPLACE(@Sort,'DESC','')
SET@operator='<='
END
ELSE
BEGIN
IFCHARINDEX('ASC',@Sort)=0
SET@strSortColumn=REPLACE(@Sort,'ASC','')
SET@operator='>='
END
IFCHARINDEX('.',@strSortColumn)>0
BEGIN
SET@SortTable=SUBSTRING(@strSortColumn,0,CHARINDEX('.',@strSortColumn))
SET@SortName=SUBSTRING(@strSortColumn,CHARINDEX('.',@strSortColumn)+1,LEN(@strSortColumn))
END
ELSE
BEGIN
SET@SortTable=@Tables
SET@SortName=@strSortColumn
END
SELECT@type=t.name,@prec=c.prec
FROMsysobjectso
JOINsyscolumnscono.id=c.id
JOINsystypestonc.xusertype=t.xusertype
WHEREo.name=@SortTableANDc.name=@SortName
IFCHARINDEX('char',@type)>0
SET@type=@type+'('+CAST(@precASvarchar)+')'
DECLARE@strPageSizevarchar(50)
DECLARE@strStartRowvarchar(50)
DECLARE@strFiltervarchar(1000)
DECLARE@strSimpleFiltervarchar(1000)
DECLARE@strGroupvarchar(1000)
/*默认当前页*/
IF@CurrentPage<1
SET@CurrentPage=1
/*设置分页参数.*/
SET@strPageSize=CAST(@PageSizeASvarchar(50))
SET@strStartRow=CAST(((@CurrentPage-1)*@PageSize+1)ASvarchar(50))
/*筛选以及分组语句.*/
IF@FilterISNOTNULLAND@Filter!=''
BEGIN
SET@strFilter='WHERE'+@Filter+''
SET@strSimpleFilter='AND'+@Filter+''
END
ELSE
BEGIN
SET@strSimpleFilter=''
SET@strFilter=''
END
IF@GroupISNOTNULLAND@Group!=''
SET@strGroup='GROUPBY'+@Group+''
ELSE
SET@strGroup=''
/*执行查询语句*/
EXEC(
'
DECLARE@SortColumn'+@type+'
SETROWCOUNT'+@strStartRow+'
SELECT@SortColumn='+@strSortColumn+'FROM'+@Tables+@strFilter+''+@strGroup+'ORDERBY'+@Sort+'
SETROWCOUNT'+@strPageSize+'
SELECT'+@Fields+'FROM'+@Tables+'WHERE'+@strSortColumn+@operator+'@SortColumn'+@strSimpleFilter+''+@strGroup+'ORDERBY'+@Sort+'
'
)
GO