Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Kesulitan dalam menulis Stored Proc untuk mengambil data untuk Jqgrid Pagination

Sepertinya saya bahwa Anda dapat memperbaiki kode dengan menggunakan OrderBy di GetFirst dan GetNext bagian dari Klausa WITH, tetapi tidak di GetAll bagian. Kode yang sesuai adalah tentang berikut

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. Ubah Bahasa Default Login di SQL Server

  2. Hapus ASCII Extended Characters 128 dan seterusnya (SQL)

  3. Ganti kemunculan pertama substring dalam string di SQL

  4. Matikan kendala sementara (MS SQL)

  5. SQL Server 2005 Penghapusan Cascading