我正在使用以下SQL过程来获取我的Web应用程序中JqGrid中用于分页的数据。
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
这里,问题是我不能在WITH子句中使用OrderBy
。但是,我需要在排序后才能获得记录。我们有什么方法可以做到这一点..
简单地说..如何实现GetAll
数据的订单
答案 0 :(得分:0)
在我看来,您可以在WITH OrderBy
和GetFirst
部分使用GetNext
来修复代码,但不 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