Sqlserver
 sql >> база данни >  >> RDS >> Sqlserver

Трудност при записването на Stored Proc за извличане на данни за Jqgrid Pagination

Струва ми се, че можете да коригирате кода с помощта на OrderBy в GetFirst и GetNext част от клаузата WITH, ноне в GetAll част. Съответният код ще бъде около следното

ALTER PROCEDURE [dbo].[NewStoredProc] 
(
    @skip int,
    @pageSize int,
    @OrderBy Varchar(20),
    @OrderByDirection Varchar(10)
 )

AS
BEGIN
    DECLARE @records int;
    SET NOCOUNT ON;
    SET @records =(select count(*) from Data where Status='A');

    IF @skip <= 0
        SELECT TOP (@pageSize) * from Data where Status='A' 
        ORDER BY CASE WHEN @OrderBy='Column1' AND @OrderByDirection='D'
                      THEN Column1
                 END DESC,
                 CASE WHEN @OrderBy='Column1' AND @OrderByDirection !='D'
                      THEN Column1
                 END,
                 CASE WHEN @OrderBy='Column2' AND @OrderByDirection='D'
                      THEN Column2
                 END DESC,
                 CASE WHEN @OrderBy='Column2' AND @OrderByDirection !='D'
                      THEN Column2
                 END,
                 CASE WHEN @OrderBy='Column3' AND @OrderByDirection='D'
                      THEN Column3
                 END DESC,
                 CASE WHEN @OrderBy='Column3' AND @OrderByDirection !='D'
                      THEN Column3
                 END
    ELSE
        WITH GetAll AS (
            SELECT * from Data where Status='A'
        ),GetFirst AS (
            SELECT TOP (@skip) *
            FROM GetAll
            ORDER BY CASE WHEN @OrderBy='Column1' AND @OrderByDirection='D'
                          THEN Column1
                     END DESC,
                     CASE WHEN @OrderBy='Column1' AND @OrderByDirection !='D'
                          THEN Column1
                     END,
                     CASE WHEN @OrderBy='Column2' AND @OrderByDirection='D'
                          THEN Column2
                     END DESC,
                     CASE WHEN @OrderBy='Column2' AND @OrderByDirection !='D'
                          THEN Column2
                     END,
                     CASE WHEN @OrderBy='Column3' AND @OrderByDirection='D'
                          THEN Column3
                     END DESC,
                     CASE WHEN @OrderBy='Column3' AND @OrderByDirection !='D'
                          THEN Column3
                     END
        ),GetNext AS (
            SELECT TOP (@pageSize) nt.*
            FROM GetAll AS nt
                LEFT OUTER JOIN GetFirst AS f ON f.Col1=nt.Col1
            WHERE f.Col1 IS  NULL
            ORDER BY CASE WHEN @OrderBy='Column1' AND @OrderByDirection='D'
                          THEN nt.Column1
                     END DESC,
                     CASE WHEN @OrderBy='Column1' AND @OrderByDirection !='D'
                          THEN nt.Column1
                     END,
                     CASE WHEN @OrderBy='Column2' AND @OrderByDirection='D'
                          THEN nt.Column2
                     END DESC,
                     CASE WHEN @OrderBy='Column2' AND @OrderByDirection !='D'
                          THEN nt.Column2
                     END,
                     CASE WHEN @OrderBy='Column3' AND @OrderByDirection='D'
                          THEN nt.Column3
                     END DESC,
                     CASE WHEN @OrderBy='Column3' AND @OrderByDirection !='D'
                          THEN nt.Column3
                     END
        )  
        SELECT * FROM GetNext

    RETURN @records;
END



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Генериране на редове въз основа на стойност на колона

  2. Как мога да принудя рамката на обекта да вмъква колони за идентичност?

  3. Проблем с честотата на преброяване на елементи в T-SQL

  4. SQL Server 2005 - Експортирайте таблицата програмно (изпълнете .sql файл, за да го изградите отново)

  5. Замяна само на n-тия екземпляр на знак