日期:2014-05-17  浏览次数:20497 次

公司业务系统的分页存储过程
老大让我研究一下分页存储过程,可惜小弟不才,只能求助各位大侠了,我在百度查了一下,这个存储过程网上也有,网上那个没有解释看不明白,这个也是,而且复杂一点 ,希望余下 --? --文字 的部分可以给出每句的解释或者那句话的作用、用途,因为本人实在太菜了
SQL code

USE [XXDB]GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
----------------------
-- Name: CT_Append Store Procedure
-- Author: xx
-- DateTime:xx
-- Description: Data TableCT_Append , Get List
-----------------------------
/*
@PageIndex 
@TotalRecords 
*/    
ALTER PROCEDURE [dbo].[CT_Append_Search]
(
    @VC_A_SNNO varchar(50),
    @VC_OC_UserName varchar(50),
    @VC_OC_Company varchar(50),
    @VC_A_CardNO varchar(50),
    @CardType int,
    @VC_A_AppendType varchar(50),
    @VC_TicketType varchar(50),
    @VC_TicketNO varchar(50),
    @StartDate varchar(50),
    @EndDate varchar(50),
    @PageIndex int,
    @TotalRecords int
)
AS
BEGIN
    DECLARE @Page int
    DECLARE @PageLowerBound int
    DECLARE @PageUpperBound int
    DECLARE @RowsToReturn int

    SET @Page = (@PageIndex - 1)    --?

    -- First set the rowcount
    SET @RowsToReturn = @TotalRecords * (@Page + 1)  --这句话有什么用
    SET ROWCOUNT @RowsToReturn                        --这里是不是语法错误,还是赋值语句?

    -- Set the page bounds
    SET @PageLowerBound = @TotalRecords * @Page            --?
    SET @PageUpperBound = @PageLowerBound + @TotalRecords + 1

    -- Create a temp table to store the select results
    CREATE TABLE #PageIndex 
    (
        IndexId int IDENTITY (1, 1) NOT NULL,             --?
        VC_A_SNNO varchar(50)
    )
    Declare @ConSQL varchar(2000)
    Declare @StrSQL varchar(2000)
    set @StrSQL='INSERT INTO #PageIndex (VC_A_SNNO)    SELECT [VC_A_SNNO] FROM [CT_Append] a LEFT OUTER JOIN
                CT_OuterCard b ON a.VC_A_CardNO = b.VC_OC_CardNO LEFT OUTER JOIN
                CT_InhouseCard c ON a.VC_A_CardNO = c.VC_IC_CardNO where a.RecordNO<>0'
    set @ConSQL=''
    if(@VC_A_SNNO<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.VC_A_SNNO like ''%' + @VC_A_SNNO +'%'''
    end
    if(@VC_OC_UserName<>'')
    begin
        set @ConSQL=@ConSQL + ' and b.VC_OC_UserName like ''%' + @VC_OC_UserName+'%'''
    end
    if(@VC_OC_Company<>'')
    begin
        set @ConSQL=@ConSQL + ' and b.VC_OC_Company like ''%' + @VC_OC_Company+'%'''
    end    
    if(@VC_A_CardNO<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.VC_A_CardNO like ''%' + @VC_A_CardNO+'%'''
    end    
    if(@CardType>0)
    begin
        set @ConSQL=@ConSQL + ' and a.I_A_CardType=' + cast(@CardType as varchar(2))
    end    
    if(@VC_A_AppendType<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.VC_A_AppendType=''' + @VC_A_AppendType+''''
    end        
    if(@VC_TicketType<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.VC_TicketType=''' + @VC_TicketType+''''
    end    
    if(@VC_TicketNO<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.VC_TicketNO=''' + @VC_TicketNO+''''
    end    
    if(@StartDate<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.D_A_AppendDateTime>=''' + @StartDate+''''
    end    
    if(@EndDate<>'')
    begin
        set @ConSQL=@ConSQL + ' and a.D_A_AppendDateTime<=''' + @EndDate+''''
    end        
    Execute(@StrSQL + @ConSQL +' order by a.D_A_AppendDateTime DESC')

    
    SELECT
        c.VC_A_SNNO,
        [VC_A_AppendType] ,
        [VC_A_CardNO] ,
        [I_A_CardType] ,
        [I_A_PointToOil] ,
        [VC_TicketType] ,
        [VC_TicketNO] ,
        [DE_A_BAmount] ,
        [DE_A_AppendAmount] ,
        [DE_A_AAmount] ,
        [D_A_AppendDateTime] ,
        [VC_A_Remark] ,
        [VC_A_OperatorNO] 
    FROM [CT_Append] c , #PageIndex PageIndex      --不明白临时表在这里有什么用?
    WHERE
        c.VC_A_SNNO = PageIndex.VC_A_SNNO AND
        PageIndex.IndexID > @PageLowerBound AND
        PageIndex.IndexID < @PageUpperBound
    
    
    Execute('SELECT COUNT(VC