我遇到了使用CTE在存儲過程中排序的問題。如果我不使用case語句,那麼排序就可以工作,但我需要能夠對四列中的任何一列進行排序。CTE不排序
任何幫助將不勝感激。這是有問題的存儲過程的片段....
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
你是什麼意思「整理作品」?唯一的'order by'在'row_number()'語句中。如果沒有排序,那麼推測變量設置不正確。 – 2014-09-01 17:16:08
如果我通過註冊DESC進行訂單,那麼它可以工作。當我使用案例陳述時,戈登並沒有 – John 2014-09-01 17:18:18
,你說得對。這是參數。就像一個布偶,我將參數@Direction聲明爲varchar(3)而不是varchar(4)。哦,那是漫長的一天。感謝您的洞察力。 – John 2014-09-01 17:35:19