sqlserver 存储过程实现分页排序

create   PROCEDURE [dbo].[usp_list_product_pager]
    @page_index INT = 0 ,
    @page_size INT = 5 ,
    @total_row INT OUTPUT ,
    @Sort VARCHAR(40) ,
    @Desc VARCHAR(10)
AS
    BEGIN 
        DECLARE @start_index INT 
        --EasyUI 页序号从1开始,这里减一以修正
        SET @page_index = @page_index - 1
        SET @start_index = @page_size * @page_index
        DECLARE @table TABLE
            (
              new_index INT IDENTITY(1, 1)
                            NOT NULL ,
              id INT
            ) 
	
        SELECT  @total_row = COUNT(*)
        FROM    product WITH ( NOLOCK ) 
        
       
        INSERT  INTO @table
                ( id
		        )
                SELECT TOP ( @start_index + @page_size )
                        id
                FROM    product
                ORDER BY CASE WHEN @Sort = 'id'
                                   AND @Desc = 'desc' THEN id
                         END DESC ,
                        CASE WHEN @Sort = 'id'
                                  AND @Desc = 'asc' THEN id
                        END ASC ,
                        CASE WHEN @Sort = 'add_on'
                                  AND @Desc = 'desc' THEN add_on
                        END DESC ,
                        CASE WHEN @Sort = 'add_on'
                                  AND @Desc = 'asc' THEN add_on
                        END ASC ,
                        CASE WHEN @Sort = 'add_by'
                                  AND @Desc = 'desc' THEN add_by
                        END DESC ,
                        CASE WHEN @Sort = 'add_by'
                                  AND @Desc = 'asc' THEN add_by
                        END ASC ,
                        CASE WHEN @Sort = 'product_name'
                                  AND @Desc = 'desc' THEN product_name
                        END DESC ,
                        CASE WHEN @Sort = 'product_name'
                                  AND @Desc = 'asc' THEN product_name
                        END ASC ,
                        CASE WHEN @Sort = 'product_description'
                                  AND @Desc = 'desc' THEN product_description
                        END DESC ,
                        CASE WHEN @Sort = 'product_description'
                                  AND @Desc = 'asc' THEN product_description
                        END ASC ,
                        CASE WHEN @Sort = ' ' THEN id
                        END DESC  
		
        DELETE  @table
        WHERE   new_index <= @start_index
         
        SELECT  c.*
        FROM    product c
                JOIN @table o ON c.id = o.id
        ORDER BY o.new_index 
    
    END 

 

你可能感兴趣的:(sqlserver 存储过程实现分页排序)