where子句中的Row_Number无效的列名

时间:2018-11-29 12:24:14

标签: sql tsql stored-procedures row-number

下面,我有一个存储过程,该存储过程应仅基于参数@From和@To显示两个数字之间的记录。但是,当我运行它时,我得到一个无效的列名RecordID的错误。我希望能够运行此过程,而RecordID列会根据我认为此代码可以执行但不会运行的where子句进行调整。

 SELECT Alerts.*, ClaimRepairs.ClaimID,ClaimRepairs.VehicleRegistration,ClaimRepairs.RepairerName, ClaimRepairs.Status,Claims.HasComplaint,
     CASE 
        WHEN Deployments.DeploymentDate IS NOT NULL AND ClaimRepairs.ConfirmedOnSiteDate IS NOT NULL 
        THEN datediff(dd, Deployments.DeploymentDate, ClaimRepairs.ConfirmedOnSiteDate) - (datediff(wk, Deployments.DeploymentDate, ClaimRepairs.ConfirmedOnSiteDate) * 2) -
            case
                when datepart(dw, Deployments.DeploymentDate) = 1 then 1 else 0 end +
            case 
                when datepart(dw, ClaimRepairs.ConfirmedOnSiteDate) = 1 then 1 else 0 end
        WHEN Deployments.DeploymentDate IS NOT NULL AND ClaimRepairs.ConfirmedOnSiteDate IS NULL
        THEN datediff(dd, Deployments.DeploymentDate,GETDATE()) - (datediff(wk, Deployments.DeploymentDate, GETDATE()) * 2) -
            case 
                when datepart(dw, Deployments.DeploymentDate) = 1 then 1 else 0 end +
            case
                when datepart(dw, GETDATE()) = 1 then 1 else 0 end
        WHEN MRD.DeploymentDate IS NOT NULL AND ClaimRepairs.ConfirmedOnSiteDate IS NOT NULL
            THEN datediff(dd, MRD.DeploymentDate, ClaimRepairs.ConfirmedOnSiteDate) - (datediff(wk, MRD.DeploymentDate, ClaimRepairs.ConfirmedOnSiteDate) * 2) -
            case
                when datepart(dw, MRD.DeploymentDate) = 1 then 1 else 0 end +
            case 
                when datepart(dw, ClaimRepairs.ConfirmedOnSiteDate) = 1 then 1 else 0 end
            WHEN MRD.DeploymentDate IS NOT NULL AND ClaimRepairs.ConfirmedOnSiteDate IS NULL
        THEN datediff(dd, MRD.DeploymentDate,GETDATE()) - (datediff(wk, MRD.DeploymentDate, GETDATE()) * 2) -
            case 
                when datepart(dw, MRD.DeploymentDate) = 1 then 1 else 0 end +
            case
                when datepart(dw, GETDATE()) = 1 then 1 else 0 end
     END AS DaysBetweenDeploymentDateOnSite,
     CASE 
        WHEN ClaimRepairs.ConfirmedOnSiteDate IS NOT NULL AND ClaimRepairs.WorkStartedDate IS NOT NULL
        THEN datediff(dd, ClaimRepairs.ConfirmedOnSiteDate, ClaimRepairs.WorkStartedDate) - (datediff(wk,ClaimRepairs.ConfirmedOnSiteDate, ClaimRepairs.WorkStartedDate) * 2) -
            case
                 when datepart(dw, ClaimRepairs.ConfirmedOnSiteDate) = 1 then 1 else 0 end +
            case
                 when datepart(dw, ClaimRepairs.WorkStartedDate) = 1 then 1 else 0 end
        WHEN ClaimRepairs.ConfirmedOnSiteDate IS NOT NULL AND ClaimRepairs.WorkStartedDate IS NULL
        THEN datediff(dd, ClaimRepairs.ConfirmedOnSiteDate, GETDATE()) - (datediff(wk,ClaimRepairs.ConfirmedOnSiteDate, GETDATE()) * 2) -
            case
                 when datepart(dw, ClaimRepairs.ConfirmedOnSiteDate) = 1 then 1 else 0 end +
            case
                 when datepart(dw, GETDATE()) = 1 then 1 else 0 end
     END AS DaysBetweenOnSiteAndDateStarted,
     ROW_NUMBER() OVER(ORDER BY Alerts.AlertID DESC) AS RecordID
    FROM ALERTS
    INNER JOIN ClaimRepairs ON Alerts.ClaimRepairID = ClaimRepairs.ClaimRepairID
    LEFT OUTER JOIN Claims ON ClaimRepairs.ClaimID = Claims.ClaimID
    LEFT OUTER JOIN IDSSearches ON Claims.ClaimID = IDSSearches.ClaimID
    LEFT OUTER JOIN Deployments ON IDSSearches.DeploymentID = Deployments.DeploymentID
    LEFT OUTER JOIN ManufacturerRepairerDeployments MRD ON IDSSearches.ManufacturerRepairerDeploymentID = MRD.ManufacturerRepairerDeploymentID
    LEFT OUTER JOIN ManufacturerApprovedRepairers MAR ON MRD.ApprovedRepairerID = MAR.ApprovedRepairerId
    LEFT OUTER JOIN SiteProfiles ON Deployments.SiteProfileID = SiteProfiles.SiteProfileID
    LEFT OUTER JOIN Actions ON Alerts.AlertID = Actions.AlertID  
    WHERE RecordID >= ''' +  convert(nvarchar(50), @From) + ''' AND RecordID < ''' +  convert(nvarchar(50), @To) + ''' AND Alerts.AlertType != 1 AND Alerts.AlertType != 2 AND Alerts.AlertType != 3  AND (Alerts.Status = 0 AND Alerts.FollowUpDate <= GETDATE() OR Alerts.Status = 0 AND Alerts.FollowUpDate IS NULL)'

IF (@WorkProviderCode != '')
BEGIN
    SET @SQL_STATEMENT = @SQL_STATEMENT + ' AND (ClaimRepairs.WorkProviderCode LIKE ''' + @WorkProviderCode + ''' OR Claims.WorkProviderCode LIKE ''' + @WorkProviderCode + ''')'
END

以前,我具有下面的Row_Number计数,但是该计数有效,但是我需要where子句来动态匹配过程底部的where子句。

FROM (SELECT Alerts.*,
         (ROW_NUMBER() OVER(ORDER BY Alerts.AlertID DESC)) AS RecordID
    FROM Alerts 
    WHERE Alerts.AlertType != 1 AND Alerts.AlertType != 2 AND Alerts.AlertType != 3  AND (Alerts.Status = 0 AND Alerts.FollowUpDate <= GETDATE() OR Alerts.Status = 0 AND Alerts.FollowUpDate IS NULL)
   ) AS A

1 个答案:

答案 0 :(得分:1)

正如您已经在注释中告诉您的那样,您不能在WHERE子句中使用别名列的名称。解决此问题的常见方法是使用通用表表达式(cte):

;WITH cte AS
(
    SELECT ...
          ,ROW_NUMBER() OVER(...) AS RecordID --aliased column
    Your query here without the WHERE-clause
)
SELECT * 
FROM cte
WHERE RecordID >= ...

顺便说一句:您的代码似乎是动态生成的语句的一部分,并且在显示方式上无效。从这一部分,我可以看到没有采取这种方法的真正原因...

在极少数情况下(行很多),出于性能原因这可能是必需的,但是我对此表示怀疑...