CreatePROCEDURE[dbo].[MS_Pagination] /**//************************************************************** ** 千万数量级分页存储过程 ** *************************************************************** 参数说明: 1.Tables :表名称,视图,不带dbo. 2.PrimaryKey :主关键字 3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc (Desc Asc前必须加空格) 4.CurrentPage :当前页码 5.PageSize :分页尺寸 6.Fields: 字段名 7.Filter :过滤语句,不带Where 8.Group :Group语句,不带Group By ***************************************************************/ ( @Tablesvarchar(1000), @PrimaryKeyvarchar(100), @Sortvarchar(200) =NULL, @CurrentPageint=1, @PageSizeint=20, @Fieldsvarchar(1000) ='*', @Filtervarchar(1000) =NULL, @Groupvarchar(1000) =NULL ) AS SET NOCOUNT ON /**//*默认排序*/ SET@Sort=RTRIM( LTRIM(@Sort) ) SET@Sort=REPLACE( REPLACE(@Sort, '[', ''),']','') IF@SortISNULLor@Sort='' SET@Sort=@PrimaryKey DECLARE@SortTablevarchar(100) DECLARE@SortNamevarchar(100) DECLARE@strSortColumnvarchar(200) DECLARE@operatorchar(2) DECLARE@typevarchar(100) DECLARE@precint DECLARE@SQL1varchar(4000),@SQL2varchar(4000) /**//*设定排序语句.*/ 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 FROM sysobjects o JOIN syscolumns c on o.id=c.id JOIN systypes t on c.xusertype=t.xusertype Where o.name =@SortTableAND c.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!='' BEGIN SET@strGroup=' GROUP BY '+@Group+'' END ELSE BEGIN SET@strGroup='' END /**//*执行查询语句*/ IF@CurrentPage=1 BEGIN SET@SQL1='Select top '+@strPageSize+''+@Fields+' FROM '+@Tables+@strFilter+@strGroup+' orDER BY '+@Sort SET@SQL2='' END ELSE BEGIN --分段 SET@SQL1='DECLARE @S '+@type+' SET ROWCOUNT '+@strStartRow+' Select @S='+@strSortColumn+' FROM '+@Tables+@strFilter+@strGroup+' orDER BY '+@Sort SET@SQL2=' SET ROWCOUNT '+@strPageSize+' Select '+@Fields+' FROM '+@Tables+' Where '+@strSortColumn+@operator+'@S '+@strSimpleFilter+''+@strGroup+'ORDER BY '+@Sort+'' END PRINT (@SQL1+@SQL2) EXEC (@SQL1+@SQL2) GO ------------------------
二,双表分页
CreatePROCEDURE dbo.MS_Pagination_OutJoin ** 千万数量级分页存储过程--带外连表 ** *************************************************************** 参数说明: 1.Tables :表名称,视图,不带dbo. 2.PrimaryKey :主关键字 3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc (Desc Asc前必须加空格) 4.CurrentPage :当前页码 5.PageSize :分页尺寸 6.Fields: 字段名 如:a.id,a.useid,a.*,b.vipname 7.Filter :过滤语句,不带Where 8.Group :Group语句,不带Group By 9.@OutTable Wap_Consort_vipInfo 10.@OutJoin a.VIPID=b.VIPID ***************************************************************/ ( @Tablesvarchar(1000), @PrimaryKeyvarchar(100), @Sortvarchar(200) =NULL, @CurrentPageint=1, @PageSizeint=20, @Fieldsvarchar(1000) ='*', @Filtervarchar(1000) =NULL, @Groupvarchar(1000) =NULL, @OutTablevarchar (100) =NULL, @OutJoinvarchar (100) =NULL ) AS /**//*默认排序*/ SET@Sort=RTRIM( LTRIM(@Sort) ) IF@SortISNULLor@Sort='' SET@Sort=@PrimaryKey DECLARE@SortTablevarchar(100) DECLARE@SortNamevarchar(100) DECLARE@strSortColumnvarchar(200) DECLARE@operatorchar(2) DECLARE@typevarchar(100) DECLARE@precint DECLARE@SQL1varchar(4000),@SQL2varchar(4000) /**//*设定排序语句.*/ 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 FROM sysobjects o JOIN syscolumns c on o.id=c.id JOIN systypes t on c.xusertype=t.xusertype Where o.name =@SortTableAND c.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!='' BEGIN SET@strGroup=' GROUP BY '+@Group+'' END ELSE BEGIN SET@strGroup='' END /**//*执行查询语句*/ --分段 SET@SQL1='DECLARE @S '+@type+' SET ROWCOUNT '+@strStartRow+' Select @S='+@strSortColumn+' FROM '+@Tables+' a '+@strFilter+@strGroup+'ORDER BY '+@Sort SET@SQL2=' SET ROWCOUNT '+@strPageSize+' Select '+@Fields+' FROM '+@Tables+' a LEFT OUTER JOIN '+@OutTable+' b on '+@OutJoin+' Where a.'+@strSortColumn+@operator+'@S '+@strSimpleFilter+''+@strGroup+'ORDER BY a.'+@Sort PRINT (@SQL1+@SQL2) EXEC (@SQL1+@SQL2) GO
三:三表分页
CreatePROCEDURE dbo.MS_Pagination_OutJoin2 ** 千万数量级分页存储过程--带两张外连表 ** *************************************************************** 参数说明: 1.Tables :表名称,视图,不带dbo. 第一张主表 2.PrimaryKey :主关键字 3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc (Desc Asc前必须加空格) 4.CurrentPage :当前页码 5.PageSize :分页尺寸 6.Fields: 字段名 如:a.id,a.useid,a.*,b.vipname 7.Filter :过滤语句,不带Where 8.Group :Group语句,不带Group By 9.@OutTable Wap_Consort_vipInfo 第二张表 10.@OutJoin a.VIPID=b.VIPID 11.@OutTable2 第三张表 12.@OutJoin2 a.UID=c.UID ***************************************************************/ ( @Tablesvarchar(100), @PrimaryKeyvarchar(100), @Sortvarchar(200) =NULL, @CurrentPageint=1, @PageSizeint=20, @Fieldsvarchar(1000) ='*', @Filtervarchar(1000) =NULL, @Groupvarchar(1000) =NULL, @OutTablevarchar (100) =NULL, @OutJoinvarchar (100) =NULL, @OutTable2varchar (100) =NULL, @OutJoin2varchar (100) =NULL ) AS /**//*默认排序*/ SET@Sort=RTRIM( LTRIM(@Sort) ) IF@SortISNULLor@Sort='' SET@Sort=@PrimaryKey DECLARE@SortTablevarchar(100) DECLARE@SortNamevarchar(100) DECLARE@strSortColumnvarchar(200) DECLARE@operatorchar(2) DECLARE@typevarchar(100) DECLARE@precint DECLARE@SQL1varchar(4000), @SQL2varchar(4000),@SQL3varchar(4000) /**//*设定排序语句.*/ 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 FROM sysobjects o JOIN syscolumns c on o.id=c.id JOIN systypes t on c.xusertype=t.xusertype Where o.name =@SortTableAND c.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!='' BEGIN SET@strGroup=' GROUP BY '+@Group+'' END ELSE BEGIN SET@strGroup='' END /**//*执行查询语句*/ --分段 SET@SQL1='DECLARE @S '+@type+' SET ROWCOUNT '+@strStartRow+' Select @S='+@strSortColumn+' FROM '+@Tables+' a '+@strFilter+@strGroup+'ORDER BY '+@Sort SET@SQL2=' SET ROWCOUNT '+@strPageSize+' Select '+@Fields+' FROM '+@Tables+' a LEFT OUTER JOIN '+@OutTable+' b on '+@OutJoin+' LEFT OUTER JOIN '+@OutTable2+' c on '+@OutJoin2 SET@SQL3=' Where a.'+@strSortColumn+@operator+'@S '+@strSimpleFilter+''+@strGroup+'ORDER BY a.'+@Sort --PRINT (@SQL1+@SQL2+@SQL3) EXEC (@SQL1+@SQL2+@SQL3) GO