SQL2005 分页存储过程

-- SQL2005 分页存储过程

CREATE PROCEDURE [ dbo ] . [ GetRecordFromPage2005 ]
   
@SelectList             VARCHAR ( 2000 ),    -- 欲选择字段列表
    @TableSource         VARCHAR ( 100 ),    -- 表名或视图表
    @SearchCondition     VARCHAR ( 2000 ),    -- 查询条件
    @OrderExpression     VARCHAR ( 1000 ),    -- 排序表达式
    @PageIndex             INT = 1 ,        -- 页号,从0开始
    @PageSize             INT = 10         -- 页尺寸
AS
BEGIN
   
IF @SelectList IS NULL OR LTRIM ( RTRIM ( @SelectList )) = ''
   
BEGIN
       
SET @SelectList = ' * '
   
END
   
PRINT @SelectList
   
   
SET @SearchCondition = ISNULL ( @SearchCondition , '' )
   
SET @SearchCondition = LTRIM ( RTRIM ( @SearchCondition ))
   
IF @SearchCondition <> ''
   
BEGIN
       
IF UPPER ( SUBSTRING ( @SearchCondition , 1 , 5 )) <> ' WHERE '
       
BEGIN
           
SET @SearchCondition = ' WHERE ' + @SearchCondition
       
END
   
END
   
PRINT @SearchCondition

   
SET @OrderExpression = ISNULL ( @OrderExpression , '' )
   
SET @OrderExpression = LTRIM ( RTRIM ( @OrderExpression ))
   
IF @OrderExpression <> ''
   
BEGIN
       
IF UPPER ( SUBSTRING ( @OrderExpression , 1 , 5 )) <> ' WHERE '
       
BEGIN
           
SET @OrderExpression = ' ORDER BY ' + @OrderExpression
       
END
   
END
   
PRINT @OrderExpression

   
IF @PageIndex IS NULL OR @PageIndex < 1
   
BEGIN
       
SET @PageIndex = 1
   
END
   
PRINT @PageIndex
   
IF @PageSize IS NULL OR @PageSize < 1
   
BEGIN
       
SET @PageSize = 10
   
END
   
PRINT   @PageSize

   
DECLARE @SqlQuery VARCHAR ( 4000 )

   
SET @SqlQuery = ' SELECT ' + @SelectList + ' ,RowNumber
    FROM
        (SELECT
' + @SelectList + ' ,ROW_NUMBER() OVER( ' + @OrderExpression + ' ) AS RowNumber
          FROM
' + @TableSource + ' ' + @SearchCondition + ' ) AS RowNumberTableSource
    WHERE RowNumber BETWEEN
' + CAST ((( @PageIndex - 1 ) * @PageSize + 1 ) AS VARCHAR )
   
+ ' AND ' +
   
CAST (( @PageIndex * @PageSize ) AS VARCHAR )
--     ORDER BY ' + @OrderExpression
    PRINT @SqlQuery
   
SET NOCOUNT ON
   
EXECUTE ( @SqlQuery )
   
SET NOCOUNT OFF

   
RETURN @@RowCount
END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值