3

我该怎么做才能RowssWHERE子句中使用名称?

SELECT TOP 10 
    ROW_NUMBER() OVER(ORDER BY dp.IdPytanie) AS Rowss,
    dp.IdPytanie
    ,dp.SpecjalnePytanie
 FROM dodajtemat_pytanie dp
 WHERE 
    (@RowBegining = 0 OR convert(int,Rowss) >= @RowBegining)
    AND (@RowEnd = 0 OR Rowss <= @RowEnd)

错误 在此处输入图像描述

这项工作->

 @RowEnd = 0 OR ROW_NUMBER() OVER(ORDER BY dp.IdPytanie) <= @RowEnd
4

1 回答 1

7

使用这样的子查询或CTE

WITH CTE
AS
(
    SELECT 
      ROW_NUMBER() OVER(ORDER BY dp.IdPytanie) AS Rowss,
      dp.IdPytanie,
      dp.SpecjalnePytanie
    FROM dodajtemat_pytanie dp
)
SELECT *
FROM CTE
WHERE (@RowBegining = 0 OR Rowss >= @RowBegining)
  AND (@RowEnd      = 0 OR Rowss <= @RowEnd);

WHERE子句在语句之前进行逻辑评估SELECT,因此它无法识别新创建的 alias Rowss

有关 SQL Server 中的逻辑查询处理步骤的详细信息,请参阅:

于 2013-03-13T12:47:36.187 回答