我有以下SQL存储过程。
DECLARE @DepotID INT = 0
DECLARE @UserID INT = 72
SELECT EM.EmployeeSurname + ', ' + EM.EmployeeForename + ' ' + UPPER(LEFT(EM.EmployeeForename2,1)) EmployeeName
, EM.EmployeeMasterPayrollNumber
, PF.FrequencyDesc
, EM.EmpCode
, DPT.DepartmentName
, D.DepotDepotDescription
, CAST(EHE.EmployeeHolidayEntitlementRemainingEntitlementDays AS INT) EmployeeHolidayEntitlementRemainingEntitlementDays
, CAST(EHE.EmployeeHolidayEntitlementDaysAdjustment AS INT) EmployeeHolidayEntitlementDaysAdjustment
, CAST(EHE.EmployeeHolidayEntitlementHolidayTakenDays AS INT) EmployeeHolidayEntitlementHolidayTakenDays
, CAST(EHE.EmployeeHolidayEntitlementHolidayBuyBack AS INT) EmployeeHolidayEntitlementHolidayBuyBack
, EHE.EmployeeHolidayEntitlementComments
, CAST(EHE.EmployeeHolidayEntitlementCarriedForwardHolidayDays AS INT) EmployeeHolidayEntitlementCarriedForwardHolidayDays
, CAST(EHE.EmployeeHolidayEntitlementBankHoliday AS INT) EmployeeHolidayEntitlementBankHoliday
, CAST(EHE.EmployeeHolidayEntitlementBaseEntitlementDays AS INT) EmployeeHolidayEntitlementBaseEntitlementDays
FROM (((ttimport.EmployeeMaster EM
LEFT OUTER JOIN ttimport.PayrollFrequency PF ON EM.FrequencyDesc = PF.DescCode)
LEFT OUTER JOIN ttimport.Departments DPT ON EM.DepartmentName = DPT.DescCode)
LEFT OUTER JOIN ttimport.EmployeeHolidayEntitlement EHE ON EM.EmpCode = EHE.EmpCode)
LEFT OUTER JOIN ttimport.Depot D ON EM.DepotDepotDescription = D.DescCode
WHERE D.DescCode IN (SELECT DepotID FROM maintenance.UserDepot WHERE UserID = @UserID)
OR (D.DescCode = @DepotID)
AND EM.EmployeeMasterLeavingDate IS NULL
首次调用时,@ DepotID设置为0.但可以再次调用@DepotID可以设置为Int值。
当@DepotID设置为0并且@UserID中的值运行该过程时,它工作正常,但是当我向@DepotID添加一个值时,它会生成比它应该多得多的记录。
我知道WHERE子句存在问题,但无论我尝试什么,我似乎都无法正常工作。
我还在WHERE子句中尝试了以下内容:
WHERE D.DescCode = @DepotID
OR D.DescCode IN (SELECT DepotID FROM maintenance.UserDepot WHERE UserID = @UserID)