Ниже у меня есть хранимая процедура, которая должна показывать записи только между двумя числами на основе параметров @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