简体   繁体   English

如何绕过 SQL Server 存储过程中的日期参数以取回所有数据

[英]How to get around date parameter in SQL Server stored procedure to get back all data

I am currently using a getShifts stored procedure to get data for 2 separate features on the front end.我目前正在使用getShifts存储过程来获取前端 2 个独立功能的数据。 My stored procedure requires a parameter Selected Date which when passed a date returns records for the specific date.我的存储过程需要一个参数Selected Date ,它在传递日期时返回特定日期的记录。

I am trying to bypass the ShiftDate parameter for one of my features and have it act as a get all.我正在尝试为我的一项功能绕过ShiftDate参数,并将其用作获取全部功能。 Is there a way to pass a fake date to the stored procedure in order to receive all records?有没有办法将假日期传递给存储过程以接收所有记录?

BEGIN
    SELECT 
        n.ID, n.ID, m.SearchFullNameFL as ProfileName, n.SchedID, 
        s.LicenseID, f.ID as UnitID, f.UnitName, 
        n.Hours, s.ShiftDate, s.SchedShiftID, ss.Description as SchedShift,  
        ss.Hours as SchedShiftHours, t.ID as TransactionTypeId,
        (SELECT COUNT(b.ID) 
         FROM Sched b
         INNER JOIN Ref_SchedShift ss ON ss.ID = b.SchedShiftID AND ss.DSN > 0
         WHERE b.ExpiredBy = 0 AND b.ShiftDate = @ShiftDate -->= @StartDate 
           AND b.EndDate <= @EndDate
           AND b.FacUnitID IN (SELECT Id FROM FacUnit 
                               WHERE FacilityID = @FacilityID) 
           AND b.ID = s.ID) AS ShiftsWorked,
        n.OverTimeReasonTypeID, n.Comment, n.Ref_NoLunchID as NoLunchID, 
        nl.NoLunchType
    FROM
        NewClockIns n
    INNER JOIN 
        Members m ON m.ID = n.ID
    INNER JOIN 
        Sched s ON s.ID = n.SchedID AND s.ExpiredBy = 0
    INNER JOIN 
        Ref_Method rm ON n.Ref_MethodID = rm.ID
    INNER JOIN 
        Ref_ClockTransactionType t ON t.ID = n.Ref_ClockTransID
    INNER JOIN 
        FacUnit f ON f.ID = n.FacUnitID
    INNER JOIN 
        Ref_License l ON l.ID = s.LicenseID
    INNER JOIN 
        Ref_SchedShift ss ON ss.Id = s.SchedShiftID
    LEFT JOIN 
        Ref_NoLunch nl ON nl.ID = n.Ref_NoLunchID
    WHERE  
        s.ShiftDate = @ShiftDate-->= @StartDate 
        AND s.EndDate <= @EndDate 
        AND s.ClockInsAgencyApprovedByID > 0 
        AND ((@IsApproved = 1 AND s.ClockInsFacilityApprovedByID > 0) 
             OR 
             (@IsApproved = 0 AND s.ClockInsFacilityApprovedByID = 0)) 
        AND f.Id IN (SELECT ID FROM @UnitIDList) 
        AND n.ExpiredBy = 0
END

Are you looking for or ?你在找or

(s.ShiftDate = @ShiftDate or @ShiftDate is null)

Then NULL can be passed in and the filter is ignored.然后可以传入NULL并忽略过滤器。

尝试这个:-

ShiftDate =(Case When @IsApproved = 1 Then @ShiftDate Else null End)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM