为什么程序不返回数据?

时间:2016-01-25 13:49:02

标签: sql sql-server tsql sql-server-2012

我一直在研究这个查询,它应该返回介于指定日期范围之间的数据,但事实并非如此。它返回的数据是否在范围之间下降。

@DateTo @DateFrom属于DateTime类型;

If(@fk_UserRoles_WebUsers_RoleID = 1) BEGIN
SELECT Cases.pk_Cases_CaseID,
       Cases.CaseTitle,
       Cases.CasePetitioner,
       Cases.CaseNo,
       Cases.CaseLetterBy,
       Cases.CaseEntryDateTime,
       CaseHearings.pk_CaseHearings_ID,
       CaseHearings.Responder,
       CaseHearings.Supervisor,
       CaseHearings.Judge,
       CaseHearings.IsOpeningDate,
       CaseHearings.IsOpeningClosingDate,
       Convert(varchar, CaseHearings.ClosingDate, 106) AS ClosingDate,
       Convert(varchar, CaseHearings.ReOpeningDate, 106) AS ReOpeningDate,
       Convert(varchar, CaseHearings.OpeningDate, 106) AS OpeningDate,
       CaseHearings.IsConveyed,
       CaseHearings.IsClosingDate,
       Convert(varchar, CaseHearings.HearingDate, 106) AS HearingDate,
       Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID,
       CaseHearings.CourtOrder,
       CaseHearings.CaseRemarks,
       ConcernedOffices.ConcernedOfficeName,
       Courts.CourtName,
       Convert(varchar,CaseHearings.GeneralDate, 106) AS GeneralDate,
       CaseHearings.IsHearingDate
FROM Cases
LEFT JOIN CaseHearings ON Cases.pk_Cases_CaseID= CaseHearings.fk_Cases_CaseHearings_CaseID
AND ((@DateFrom =''
      AND @DateTo ='')
     OR (Cases.CaseEntryDateTime >= @DateFrom
         AND Cases.CaseEntryDateTime < DATEADD(DAY, 1, @DateTo) ) )
LEFT JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID= Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID
LEFT JOIN Courts ON Courts.pk_Courts_CourtID= CaseHearings.fk_Courts_CaseHearings_CourtID
INNER JOIN Web_Users ON Web_Users.UserID= Cases.fk_WebUsers_Cases_UserID
WHERE (@CaseNo =''
       OR (@CaseNo!=''
           AND Cases.CaseNo LIKE '%'+ @CaseNo +'%'))
  AND Cases.CaseTitle LIKE '%'+@CaseTitle+'%' END

1 个答案:

答案 0 :(得分:1)

您在连接中使用了@DateFrom和@DateTo,这意味着在连接表之间的数据时会检查此条件。您应该在where子句中使用此条件,以便根据您的条件过滤记录。