<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
效果演示: http://www.cn5135.com/_App/Enterprise/QueryResult.aspx
一组MSSQL千万数量级分页存储过程
一:单表分页
------------------------
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
二,双表分页
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
三:三表分页
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
效果演示: http://www.cn5135.com/_App/Enterprise/QueryResult.aspx
CREATE
PROCEDURE
CN5135_SP_Pagination
/**/ /*
***************************************************************
** 千万数量级分页存储过程 **
***************************************************************
参数说明:
1.Tables :表名称,视图
2.PrimaryKey :主关键字
3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc
4.CurrentPage :当前页码
5.PageSize :分页尺寸
6.Filter :过滤语句,不带Where
7.Group :Group语句,不带Group By
效果演示:http://www.cn5135.com/_App/Enterprise/QueryResult.aspx
***************************************************************/
(
@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
/**/ /*设定排序语句.*/
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 , 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 = @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 + '
'
)
GO
/**/ /*
***************************************************************
** 千万数量级分页存储过程 **
***************************************************************
参数说明:
1.Tables :表名称,视图
2.PrimaryKey :主关键字
3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc
4.CurrentPage :当前页码
5.PageSize :分页尺寸
6.Filter :过滤语句,不带Where
7.Group :Group语句,不带Group By
效果演示:http://www.cn5135.com/_App/Enterprise/QueryResult.aspx
***************************************************************/
(
@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
/**/ /*设定排序语句.*/
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 , 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 = @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 + '
'
)
GO
一组MSSQL千万数量级分页存储过程
一:单表分页
Create
PROCEDURE
[
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
***************************************************************/
(
@Tables varchar ( 1000 ),
@PrimaryKey varchar ( 100 ),
@Sort varchar ( 200 ) = NULL ,
@CurrentPage int = 1 ,
@PageSize int = 20 ,
@Fields varchar ( 1000 ) = ' * ' ,
@Filter varchar ( 1000 ) = NULL ,
@Group varchar ( 1000 ) = NULL
)
AS
SET NOCOUNT ON
/**/ /*默认排序*/
SET @Sort = RTRIM ( LTRIM ( @Sort ) )
SET @Sort = REPLACE ( REPLACE ( @Sort , ' [ ' , '' ), ' ] ' , '' )
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 @SQL1 varchar ( 4000 ), @SQL2 varchar ( 4000 )
/**/ /*设定排序语句.*/
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 , 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 = @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 != ''
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
/**/ /**************************************************************
** 千万数量级分页存储过程 **
***************************************************************
参数说明:
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
***************************************************************/
(
@Tables varchar ( 1000 ),
@PrimaryKey varchar ( 100 ),
@Sort varchar ( 200 ) = NULL ,
@CurrentPage int = 1 ,
@PageSize int = 20 ,
@Fields varchar ( 1000 ) = ' * ' ,
@Filter varchar ( 1000 ) = NULL ,
@Group varchar ( 1000 ) = NULL
)
AS
SET NOCOUNT ON
/**/ /*默认排序*/
SET @Sort = RTRIM ( LTRIM ( @Sort ) )
SET @Sort = REPLACE ( REPLACE ( @Sort , ' [ ' , '' ), ' ] ' , '' )
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 @SQL1 varchar ( 4000 ), @SQL2 varchar ( 4000 )
/**/ /*设定排序语句.*/
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 , 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 = @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 != ''
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
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
二,双表分页
Create
PROCEDURE
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
***************************************************************/
(
@Tables varchar ( 1000 ),
@PrimaryKey varchar ( 100 ),
@Sort varchar ( 200 ) = NULL ,
@CurrentPage int = 1 ,
@PageSize int = 20 ,
@Fields varchar ( 1000 ) = ' * ' ,
@Filter varchar ( 1000 ) = NULL ,
@Group varchar ( 1000 ) = NULL ,
@OutTable varchar ( 100 ) = NULL ,
@OutJoin varchar ( 100 ) = NULL
)
AS
/**/ /*默认排序*/
SET @Sort = RTRIM ( LTRIM ( @Sort ) )
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 @SQL1 varchar ( 4000 ), @SQL2 varchar ( 4000 )
/**/ /*设定排序语句.*/
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 , 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 = @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 != ''
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
** 千万数量级分页存储过程 -- 带外连表 **
***************************************************************
参数说明:
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
***************************************************************/
(
@Tables varchar ( 1000 ),
@PrimaryKey varchar ( 100 ),
@Sort varchar ( 200 ) = NULL ,
@CurrentPage int = 1 ,
@PageSize int = 20 ,
@Fields varchar ( 1000 ) = ' * ' ,
@Filter varchar ( 1000 ) = NULL ,
@Group varchar ( 1000 ) = NULL ,
@OutTable varchar ( 100 ) = NULL ,
@OutJoin varchar ( 100 ) = NULL
)
AS
/**/ /*默认排序*/
SET @Sort = RTRIM ( LTRIM ( @Sort ) )
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 @SQL1 varchar ( 4000 ), @SQL2 varchar ( 4000 )
/**/ /*设定排序语句.*/
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 , 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 = @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 != ''
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
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
三:三表分页
Create
PROCEDURE
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
***************************************************************/
(
@Tables varchar ( 100 ),
@PrimaryKey varchar ( 100 ),
@Sort varchar ( 200 ) = NULL ,
@CurrentPage int = 1 ,
@PageSize int = 20 ,
@Fields varchar ( 1000 ) = ' * ' ,
@Filter varchar ( 1000 ) = NULL ,
@Group varchar ( 1000 ) = NULL ,
@OutTable varchar ( 100 ) = NULL ,
@OutJoin varchar ( 100 ) = NULL ,
@OutTable2 varchar ( 100 ) = NULL ,
@OutJoin2 varchar ( 100 ) = NULL
)
AS
/**/ /*默认排序*/
SET @Sort = RTRIM ( LTRIM ( @Sort ) )
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 @SQL1 varchar ( 4000 ), @SQL2 varchar ( 4000 ), @SQL3 varchar ( 4000 )
/**/ /*设定排序语句.*/
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 , 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 = @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 != ''
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
** 千万数量级分页存储过程 -- 带两张外连表 **
***************************************************************
参数说明:
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
***************************************************************/
(
@Tables varchar ( 100 ),
@PrimaryKey varchar ( 100 ),
@Sort varchar ( 200 ) = NULL ,
@CurrentPage int = 1 ,
@PageSize int = 20 ,
@Fields varchar ( 1000 ) = ' * ' ,
@Filter varchar ( 1000 ) = NULL ,
@Group varchar ( 1000 ) = NULL ,
@OutTable varchar ( 100 ) = NULL ,
@OutJoin varchar ( 100 ) = NULL ,
@OutTable2 varchar ( 100 ) = NULL ,
@OutJoin2 varchar ( 100 ) = NULL
)
AS
/**/ /*默认排序*/
SET @Sort = RTRIM ( LTRIM ( @Sort ) )
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 @SQL1 varchar ( 4000 ), @SQL2 varchar ( 4000 ), @SQL3 varchar ( 4000 )
/**/ /*设定排序语句.*/
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 , 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 = @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 != ''
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