我正在使用以下 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
在这里,问题是我不能OrderBy
在 WITH 子句中使用。但是,我只需要在排序后获取记录。有什么办法可以做到这一点..
简单地说..如何实现GetAll
数据排序