我在使用CTE对存储过程进行排序时遇到问题。如果我不使用case语句,那么排序有效,但我需要能够对四列中的任何一列进行排序。
任何帮助都将非常感激。以下是有问题的存储过程的片段....
DECLARE @ValuationIds ValuationIdTableType
;WITH CTE
AS
(
SELECT V.[Id], Registration, (ROW_NUMBER() OVER
(
ORDER BY
CASE WHEN @SortCol='Registration' and @Direction='DESC' then Registration END DESC,
CASE WHEN @SortCol='Registration' and @Direction='ASC' then Registration END ASC
)) AS seq
FROM [Valuation] V WITH (NOLOCK)
INNER JOIN dbo.Vehicle a ON a.Id = V.VehicleId
INNER JOIN dbo.Derivative b on b.Id = a.DerivativeId
INNER JOIN dbo.Model c on c.Id = b.ModelId
INNER JOIN dbo.ModelRange d on d.Id = c.ModelRangeId
INNER JOIN dbo.Manufacturer e on e.Id = d.ManufacturerId
LEFT JOIN dbo.Disposal f ON f.ValuationId = V.Id
CROSS APPLY (
SELECT TOP 1 *
FROM dbo.SellerAdvanceValuation savCA
INNER JOIN udf_SplitIDs(@CentreIdList) IdList
ON IdList.EntityID = savCA.CentreId
WHERE V.Id = savCA.ValuationId
ORDER BY Id DESC
) sav
WHERE [Deleted] = 0
AND f.Id IS NULL
AND COALESCE(@Registration, [Registration]) = [Registration]
AND COALESCE(@VehicleStatusId,[VehicleStatusId] ) = [VehicleStatusId]
AND COALESCE(@StartDate,sav.RequestDateTime ) <= sav.RequestDateTime
AND sav.RequestDateTime <= COALESCE(@EndDate,sav.RequestDateTime )
AND sav.Amount IS NOT NULL
AND sav.ValuationDateTime IS NOT NULL
AND sav.ValuationDateTime > @ValuationExpiryDateTime
)
INSERT INTO @ValuationIds(ValuationId)
SELECT a.Id
FROM CTE a
WHERE seq BETWEEN @StartIndex AND (@StartIndex + @MaxRows - 1)
EXEC dbo.Valuation_Get_All @ValuationIds
更新: 如果我做了以下它可以工作,但我真的不想这样做。我有4列,必须在每列上升和下降。
DECLARE @ValuationIds ValuationIdTableType
;WITH CTE
AS
(
SELECT V.[Id], Registration, (ROW_NUMBER() OVER
(
ORDER BY Registration DESC
--CASE WHEN @SortCol='Registration' and @Direction='DESC' then Registration END DESC,
--CASE WHEN @SortCol='Registration' and @Direction='ASC' then Registration END ASC
)) AS seq
FROM [Valuation] V WITH (NOLOCK)
INNER JOIN dbo.Vehicle a ON a.Id = V.VehicleId
INNER JOIN dbo.Derivative b on b.Id = a.DerivativeId
INNER JOIN dbo.Model c on c.Id = b.ModelId
INNER JOIN dbo.ModelRange d on d.Id = c.ModelRangeId
INNER JOIN dbo.Manufacturer e on e.Id = d.ManufacturerId
LEFT JOIN dbo.Disposal f ON f.ValuationId = V.Id
CROSS APPLY (
SELECT TOP 1 *
FROM dbo.SellerAdvanceValuation savCA
INNER JOIN udf_SplitIDs(@CentreIdList) IdList
ON IdList.EntityID = savCA.CentreId
WHERE V.Id = savCA.ValuationId
ORDER BY Id DESC
) sav
WHERE [Deleted] = 0
AND f.Id IS NULL
AND COALESCE(@Registration, [Registration]) = [Registration]
AND COALESCE(@VehicleStatusId,[VehicleStatusId] ) = [VehicleStatusId]
AND COALESCE(@StartDate,sav.RequestDateTime ) <= sav.RequestDateTime
AND sav.RequestDateTime <= COALESCE(@EndDate,sav.RequestDateTime )
AND sav.Amount IS NOT NULL
AND sav.ValuationDateTime IS NOT NULL
AND sav.ValuationDateTime > @ValuationExpiryDateTime
)
INSERT INTO @ValuationIds(ValuationId)
SELECT a.Id
FROM CTE a
WHERE seq BETWEEN @StartIndex AND (@StartIndex + @MaxRows - 1)
EXEC dbo.Valuation_Get_All @ValuationIds
答案 0 :(得分:0)
INSERT INTO @ValuationIds(ValuationId) 选择a.Id 来自CTE a 在哪里@WartIndex与(@StartIndex + @MaxRows - 1)之间的关系 ORDER BY { ColumnFromInCTE }
????