程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> MSsql數據庫如何實現分頁存儲的代碼實例

MSsql數據庫如何實現分頁存儲的代碼實例

編輯:關於SqlServer

    reate PROCEDURE Sp_Conn_Sort
    (
    @tblName   varchar(255),       -- 表名

    @strGetFields varchar(1000) = '*',  -- 需要返回的列

    @fldName varchar(255)='',      -- 排序的字段名

    @PageSize   int = 40,          -- 頁尺寸

    @PageIndex  int = 1,           -- 頁碼

    @doCount  bit = 0,   -- 返回記錄總數, 非 0 值則返回

    @OrderType bit = 0,  -- 設置排序類型, 非 0 值則降序
    @strWhere  varchar(1500)=''  -- 查詢條件 (注意: 不要加 where)
    )
    AS

    declare @strSQL   varchar(5000)       -- 主語句

    declare @strTmp   varchar(110)        -- 臨時變量

    declare @strOrder varchar(400)        -- 排序類型

    if @doCount != 0

      begin

        if @strWhere !=''

        set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere

        else

        set @strSQL = 'select count(*) as Total from ' + @tblName

    end

    --以上代碼的意思是如果@doCount傳遞過來的不是0,就執行總數統計。以下的所有代碼都是@doCount為0的情況

    else

    begin

    if @OrderType != 0

    begin

        set @strTmp = '<(select min'

    set @strOrder = ' order by ' + @fldName +' desc'

    --如果@OrderType不是0,就執行降序,這句很重要!

    end

    else

    begin

        set @strTmp = '>(select max'

        set @strOrder = ' order by ' + @fldName +' asc'

    end

    if @PageIndex = 1

    begin

        if @strWhere != ''

        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder

         else

         set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from '+ @tblName + ' '+ @strOrder

    --如果是第一頁就執行以上代碼,這樣會加快執行速度

    end

    else

    begin

    --以下代碼賦予了@strSQL以真正執行的SQL代碼

    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from '

        + @tblName + ' where ' + @fldName + '' + @strTmp + '('+ @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + '' + @strOrder + ') as tblTmp)'+ @strOrder

    if @strWhere != ''

        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from '

            + @tblName + ' where ' + @fldName + '' + @strTmp + '('

            + @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '

            + @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '

            + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

    end

    end

    exec (@strSQL)

    -----------------

    sql server 2000 單主鍵高效分頁存儲過程 (支持多字段排序)

    Create PROC P_viewPage       
        /**//*        
        nzperfect [no_mIss] 高效通用分頁存儲過程(雙向檢索) 2007.5.7  QQ:34813284        
        敬告:適用於單一主鍵或存在唯一值列的表或視圖        
        ps教程:Sql語句為8000字節,調用時請注意傳入參數及sql總長度不要超過指定范圍        
        */        
        @TableName VARCHAR(200),     --表名        
        @FieldList VARCHAR(2000),    --顯示列名,如果是全部字段則為*        
        @PrimaryKey VARCHAR(100),    --單一主鍵或唯一值鍵        
        @Where VARCHAR(2000),        --查詢條件 不含'where'字符,如id>10 and len(userid)>9        
        @Order VARCHAR(1000),        --排序 不含'order by'字符,如id asc,userid desc,必須指定asc或desc        
        --注意當@SortType=3時生效,記住一定要在最後加上主鍵,否則會讓你比較郁悶        
        @SortType INT,               --排序規則 1:正序asc 2:倒序desc 3:多列排序方法        
        @RecorderCount INT,          --記錄總數 0:會返回總記錄        
        @PageSize INT,               --每頁輸出的記錄數        
        @PageIndex INT,              --當前頁數        
        @TotalCount INT OUTPUT ,      --記返回總記錄        
        @TotalPageCount INT OUTPUT   --返回總頁數        
    AS        
    SET NOCOUNT ON        
        IF ISNULL(@TotalCount,'') = '' SET @TotalCount = 0        
        SET @Order = RTRIM(LTRIM(@Order))        
        SET @PrimaryKey = RTRIM(LTRIM(@PrimaryKey))        
        SET @FieldList = REPLACE(RTRIM(LTRIM(@FieldList)),' ','')        
        WHILE CHARINDEX(', ',@Order) > 0 or CHARINDEX(' ,',@Order) > 0        
            BEGIN        
                SET @Order = REPLACE(@Order,', ',',')        
                SET @Order = REPLACE(@Order,' ,',',')        
            END        
        IF ISNULL(@TableName,'') = '' or ISNULL(@FieldList,'') = ''        
                or ISNULL(@PrimaryKey,'') = ''        
                or @SortType < 1 or @SortType >3        
                or @RecorderCount  < 0 or @PageSize < 0 or @PageIndex < 0        
            BEGIN        
                PRINT('ERR_00')        
                RETURN        
            END        
        IF @SortType = 3        
            BEGIN        
                IF (UPPER(RIGHT(@Order,4))!=' ASC' AND UPPER(RIGHT(@Order,5))!=' DESC')        
                    BEGIN PRINT('ERR_02') RETURN END        
            END        
        DECLARE @new_where1 VARCHAR(1000)        
        DECLARE @new_where2 VARCHAR(1000)        
        DECLARE @new_order1 VARCHAR(1000)        
        DECLARE @new_order2 VARCHAR(1000)        
        DECLARE @new_order3 VARCHAR(1000)        
        DECLARE @Sql VARCHAR(8000)        
        DECLARE @SqlCount NVARCHAR(4000)        
        IF ISNULL(@where,'') = ''        
            BEGIN        
                SET @new_where1 = ' '        
                SET @new_where2 = ' Where  '        
            END        
        ELSE        
            BEGIN        
                SET @new_where1 = ' Where ' + @where        
                SET @new_where2 = ' Where ' + @where + ' AND '        
            END        
        IF ISNULL(@order,'') = '' or @SortType = 1  or @SortType = 2        
            BEGIN        
                IF @SortType = 1        
                    BEGIN        
                        SET @new_order1 = ' orDER BY ' + @PrimaryKey + ' ASC'        
                        SET @new_order2 = ' orDER BY ' + @PrimaryKey + ' DESC'        
                    END        
                IF @SortType = 2        
                    BEGIN        
                        SET @new_order1 = ' orDER BY ' + @PrimaryKey + ' DESC'        
                        SET @new_order2 = ' orDER BY ' + @PrimaryKey + ' ASC'        
                    END        
            END        
        ELSE        
            BEGIN        
                SET @new_order1 = ' orDER BY ' + @Order        
            END        
      
        IF @SortType = 3 AND  CHARINDEX(','+@PrimaryKey+' ',','+@Order)>0        
        BEGIN        
            SET @new_order1 = ' orDER BY ' + @Order        
            SET @new_order2 = @Order + ','        
            SET @new_order2 = REPLACE(REPLACE(@new_order2,'ASC,','{ASC},'),'DESC,','{DESC},')        
            SET @new_order2 = REPLACE(REPLACE(@new_order2,'{ASC},','DESC,'),'{DESC},','ASC,')        
            SET @new_order2 = ' orDER BY ' + SUBSTRING(@new_order2,1,LEN(@new_order2)-1)        
            IF @FieldList <> '*'        
                BEGIN        
                    SET @new_order3 = REPLACE(REPLACE(@Order + ',','ASC,',','),'DESC,',',')        
                    SET @FieldList = ',' + @FieldList        
                    WHILE CHARINDEX(',',@new_order3)>0        
                        BEGIN        
                            IF CHARINDEX(SUBSTRING(','+@new_order3,1,CHARINDEX(',',@new_order3)),','+@FieldList+',')>0        
                                BEGIN        
                                    SET @FieldList =        
                                    @FieldList + ',' + SUBSTRING(@new_order3,1,CHARINDEX(',',@new_order3))        
                                END        
                            SET @new_order3 =        
                            SUBSTRING(@new_order3,CHARINDEX(',',@new_order3)+1,LEN(@new_order3))        
                        END        
                    SET @FieldList = SUBSTRING(@FieldList,2,LEN(@FieldList))        
                END        
            END   
           
        SET @SqlCount = 'Select @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'        
        + CAST(@PageSize AS VARCHAR)+') FROM (Select * FROM ' + @TableName + @new_where1+') AS T'        
        IF @RecorderCount  = 0        
            BEGIN        
                EXEC SP_EXECUTESQL @SqlCount,N'@TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT',        
                @TotalCount OUTPUT,@TotalPageCount OUTPUT        
            END        
        ELSE        
            BEGIN        
                Select @TotalCount = @RecorderCount      
            END        
        IF @PageIndex > CEILING((@TotalCount+0.0)/@PageSize)        
            BEGIN        
                SET @PageIndex =  CEILING((@TotalCount+0.0)/@PageSize)        
            END        
        IF @PageIndex = 1 or @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize)        
            BEGIN        
                IF @PageIndex = 1 --返回第一頁數據        
                    BEGIN        
                        SET @Sql = 'Select * FROM (Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '        
                        + @TableName + @new_where1 + @new_order1 +') AS TMP ' + @new_order1  
                    END        
                IF @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize)  --返回最後一頁數據        
                    BEGIN        
                        SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('        
                        + 'Select TOP ' + STR(ABS(@PageSize*@PageIndex-@TotalCount-@PageSize))        
                        + ' ' + @FieldList + ' FROM '        
                        + @TableName + @new_where1 + @new_order2 + ' ) AS TMP '        
                        + @new_order1        
                    END        
            END        
        ELSE    
              
            BEGIN        
            IF @SortType = 1 www.3ppt.com --僅主鍵正序排序        
                BEGIN        
                    IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2  --正向檢索        
                        BEGIN        
                            SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '        
                            + @TableName + @new_where2 + @PrimaryKey + ' > '        
                            + '(Select MAX(' + @PrimaryKey + ') FROM (Select TOP '        
                            + STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey        
                            + ' FROM ' + @TableName        
                            + @new_where1 + @new_order1 +' ) AS TMP) '+ @new_order1        
                        END        
                    ELSE  --反向檢索        
                        BEGIN        
                            SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('        
                            + 'Select TOP ' + STR(@PageSize) + ' '        
                            + @FieldList + ' FROM '        
                            + @TableName + @new_where2 + @PrimaryKey + ' < '        
                            + '(Select MIN(' + @PrimaryKey + ') FROM (Select TOP '       
                            + STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey        
                            + ' FROM ' + @TableName        
                            + @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2        
                            + ' ) AS TMP ' + @new_order1        
                        END        
                END        
            IF @SortType = 2  --僅主鍵反序排序        
                BEGIN        
                    IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2  --正向檢索        
                        BEGIN        
                            SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '        
                            + @TableName + @new_where2 + @PrimaryKey + ' < '        
                            + '(Select MIN(' + @PrimaryKey + ') FROM (Select TOP '        
                            + STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey        
                            +' FROM '+ @TableName        
                            + @new_where1 + @new_order1 + ') AS TMP) '+ @new_order1        
                        END        
                    ELSE  --反向檢索        
                        BEGIN        
                            SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('        
                            + 'Select TOP ' + STR(@PageSize) + ' '        
                            + @FieldList + ' FROM '        
                            + @TableName + @new_where2 + @PrimaryKey + ' > '        
                            + '(Select MAX(' + @PrimaryKey + ') FROM (Select TOP '        
                            + STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey        
                            + ' FROM ' + @TableName        
                            + @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2        
                            + ' ) AS TMP ' + @new_order1        
                        END        
                END        
            IF @SortType = 3  --多列排序,必須包含主鍵,且放置最後,否則不處理        
                BEGIN        
                    IF CHARINDEX(',' + @PrimaryKey + ' ',',' + @Order) = 0        
                        BEGIN PRINT('ERR_02') RETURN END        
                        IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2  --正向檢索        
                            BEGIN        
                                SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '        
                                + 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '        
                                + ' Select TOP ' + STR(@PageSize*@PageIndex) + ' ' + @FieldList        
                                + ' FROM ' + @TableName + @new_where1 + @new_order1 + ' ) AS TMP '        
                                + @new_order2 + ' ) AS TMP ' + @new_order1        
                            END        
                        ELSE  --反向檢索        
                            BEGIN        
                                SET @Sql = 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '        
                                + 'Select TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '        
                                + ' Select TOP ' + STR(@TotalCount-@PageSize *@PageIndex+@PageSize) + ' ' + @FieldList        
                                + ' FROM ' + @TableName + @new_where1 + @new_order2 + ' ) AS TMP '        
                                + @new_order1 + ' ) AS TMP ' + @new_order1        
                            END        
                END        
            END        
        PRINT(@SQL)        
        EXEC(@Sql)

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved