I am using the following SQL Procedure for getting data to use for pagination in JqGrid in my web Application.
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
),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)
SELECT * FROM GetNext;
RETURN @records;
END
Here, the Problem is i can not use OrderBy
in WITH Clause. But, i need to get the records only after sorting.Is there any way we can do this..
Simply to say.. how to implement order by for GetAll
data
It seems to my that you can fix the code by usage of
OrderBy
inGetFirst
andGetNext
part of the WITH Clause, but not in theGetAll
part. The corresponding code will be about the following