通用分页存储过程2

联合主键的:

None.gif CREATE PROC P_public_ViewPage
ExpandedBlockStart.gif
/**/ /*
InBlock.gif no_mIss 通用分页存储过程 2007.3.1 QQ:34813284
InBlock.gif 适用于联合主键/单主键/存在能确定唯一行列/存在能确定唯一行的多列 (用英文,隔开)
InBlock.gif 调用:
InBlock.gif 第一页查询时返回总记录和总页数及第一页记录:
InBlock.gif EXECUTE P_public_ViewPage_per 'TableName','col1,col2,col3,col4','pk1,pk2,pk3',
InBlock.gif 'col5>0 and col7<9','pk1 asc,pk2 asc,pk3 asc',0,10,1,
InBlock.gif @TotalCount OUTPUT,@TotalPageCount OUTPUT
InBlock.gif 其它页调用,比如第89页(假设第一页查询时返回总记录为2000000):
InBlock.gif EXECUTE P_public_ViewPage_per 'TableName','col1,col2,col3,col4','pk1,pk2,pk3',
InBlock.gif 'col5>0 and col7<9','pk1 asc,pk2 asc,pk3 asc',2000000,10,89,
InBlock.gif @TotalCount OUTPUT,@TotalPageCount OUTPUT
ExpandedBlockEnd.gif
*/

None.gif
None.gif
@TableName VARCHAR ( 200 ), -- 表名
None.gif
@FieldList VARCHAR ( 2000 ), -- 显示列名
None.gif
@PrimaryKey VARCHAR ( 100 ), -- 单一主键或唯一值键或联合主键列表(用英文,隔开)或能确定唯一行的多列列表(用英文,隔开)
None.gif
@Where VARCHAR ( 1000 ), -- 查询条件 不含'where'字符
None.gif
@Order VARCHAR ( 1000 ), -- 排序 不含'order by'字符,用英文,隔开
None.gif
@RecorderCount INT , -- 记录总数 0:会返回总记录
None.gif
@PageSize INT , -- 每页输出的记录数
None.gif
@PageIndex INT , -- 当前页数
None.gif
@TotalCount INT OUTPUT, -- 返回记录总数
None.gif
@TotalPageCount INT OUTPUT -- 返回总页数
None.gif
AS
None.gif
None.gif
SET NOCOUNT ON
None.gif
None.gif
SET @FieldList = REPLACE ( @FieldList , ' ' , '' )
None.gif
IF @FieldList = ' * '
None.gif
BEGIN SET @FieldList = ' A.* ' END
None.gif
ELSE
None.gif
BEGIN
None.gif
SET @FieldList = ' A. ' + REPLACE ( @FieldList , ' , ' , ' ,A. ' )
None.gif
END
None.gif
None.gif
WHILE CHARINDEX ( ' , ' , @Order ) > 0
None.gif
BEGIN
None.gif
SET @Order = REPLACE ( @Order , ' , ' , ' , ' )
None.gif
END
None.gif
None.gif
IF ISNULL ( @TableName , '' ) = '' OR ISNULL ( @PrimaryKey , '' ) = ''
None.gif
OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
None.gif
BEGIN
None.gif
RETURN
None.gif
END
None.gif
None.gif
DECLARE @new_where1 VARCHAR ( 1000 )
None.gif
DECLARE @new_where2 VARCHAR ( 1000 )
None.gif
DECLARE @new_where3 VARCHAR ( 1000 )
None.gif
DECLARE @new_where4 VARCHAR ( 1000 )
None.gif
DECLARE @new_order1 VARCHAR ( 1000 )
None.gif
DECLARE @new_order2 VARCHAR ( 1000 )
None.gif
DECLARE @Fields VARCHAR ( 1000 )
None.gif
DECLARE @Sql VARCHAR ( 8000 )
None.gif
DECLARE @SqlCount NVARCHAR ( 4000 )
None.gif
None.gif
SET @Fields = @PrimaryKey + ' , '
None.gif
SET @new_where2 = ''
None.gif
SET @new_where4 = ''

None.gif
IF ISNULL ( @where , '' ) = ''
None.gif
BEGIN
None.gif
SET @new_where1 = ' '
None.gif
SET @new_where3 = ' WHERE '
None.gif
END
None.gif
ELSE
None.gif
BEGIN
None.gif
SET @new_where1 = ' WHERE ' + @where + ' '
None.gif
SET @new_where3 = ' WHERE 1=1 '
None.gif
+ REPLACE ( ' AND ' + @where , ' AND ' , ' AND A. ' ) + ' AND '
None.gif
END
None.gif
None.gif
WHILE CHARINDEX ( ' , ' , @Fields ) > 0
None.gif
BEGIN
None.gif
SET @new_where2 = @new_where2
None.gif
+ ' A. ' + LTRIM ( LEFT ( @Fields , CHARINDEX ( ' , ' , @Fields ) - 1 ))
None.gif
+ ' = B. ' + LTRIM ( LEFT ( @Fields , CHARINDEX ( ' , ' , @Fields ) - 1 )) + ' AND '
None.gif
SET @new_where4 = @new_where4
None.gif
+ ' B. ' + LTRIM ( LEFT ( @Fields , CHARINDEX ( ' , ' , @Fields ) - 1 )) + ' IS NULL AND '
None.gif
SET @Fields = SUBSTRING ( @Fields , CHARINDEX ( ' , ' , @Fields ) + 1 , LEN ( @Fields ))
None.gif
END
None.gif
SET @new_where2 = LEFT ( @new_where2 , LEN ( @new_where2 ) - 4 )
None.gif
SET @new_where4 = LEFT ( @new_where4 , LEN ( @new_where4 ) - 4 )
None.gif
None.gif
IF ISNULL ( @order , '' ) = ''
None.gif
BEGIN
None.gif
SET @new_order1 = ''
None.gif
SET @new_order2 = ''
None.gif
END
None.gif
ELSE
None.gif
BEGIN
None.gif
SET @new_order1 = ' ORDER BY ' + @Order
None.gif
SET @new_order2 = ' ORDER BY '
None.gif
+ RIGHT ( REPLACE ( ' , ' + @Order , ' , ' , ' , A. ' ),
None.gif
LEN ( REPLACE ( ' , ' + @Order , ' , ' , ' , A. ' )) - 1 )
None.gif
END
None.gif
None.gif
SET @SqlCount = ' SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/ '
None.gif
+ CAST ( @PageSize AS VARCHAR ) + ' ) FROM ' + @TableName
None.gif
+ ' A ' + @new_where1
None.gif
None.gif
IF @RecorderCount = 0
None.gif
BEGIN
None.gif
EXEC SP_EXECUTESQL @SqlCount ,N ' @TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT ' ,
None.gif
@TotalCount OUTPUT, @TotalPageCount OUTPUT
None.gif
END
None.gif
ELSE
None.gif
BEGIN
None.gif
SELECT @TotalCount = @RecorderCount
None.gif
END
None.gif
None.gif
IF @PageIndex > CEILING (( @TotalCount + 0.0 ) / @PageSize )
None.gif
BEGIN
None.gif
SET @PageIndex = CEILING (( @TotalCount + 0.0 ) / @PageSize )
None.gif
END
None.gif
IF @PageIndex = 1
None.gif
BEGIN
None.gif
SET @Sql = ' SELECT TOP ' + STR ( @PageSize ) + ' ' + @FieldList + ' FROM '
None.gif
+ @TableName + ' A ' + @new_where1 + @new_order1
None.gif
END
None.gif
ELSE
None.gif
BEGIN
None.gif
SET @Sql = ' SELECT TOP ' + STR ( @PageSize ) + ' ' + @FieldList + ' FROM '
None.gif
+ @TableName + ' A LEFT JOIN (SELECT TOP '
None.gif
+ STR ( @PageSize * ( @PageIndex - 1 ))
None.gif
+ ' ' + @PrimaryKey + ' FROM ' + @TableName + @new_where1
None.gif
+ @new_order1 + ' )B ON ' + @new_where2 + @new_where3
None.gif
+ @new_where4 + @new_order2
None.gif
END
None.gif
None.gif
EXEC ( @Sql )
None.gif
GO


你可能感兴趣的:(存储过程,分页,通用)