Sql where条件没有返回指定的行

时间:2012-11-05 19:01:39

标签: sql sql-server-2008-r2

我有一个sql select查询,它应该返回一堆由where子句过滤的行。一切正常,直到我添加“OR”然后它似乎没有返回正确的行。(Sql server management Studio 2008)

这是我的问题:

Select @Command = 
'select DISTINCT C.FileID, C.FileName, convert(nvarchar,C.DateReported,111) AS ''DateReported'', 
C.FileDetailsPlainText, CFIT.Level3 as ''InvestigationType'', INV.FName + '' '' + INV.LName AS ''Name'', 
convert(nvarchar,DD,111) AS ''DD'', i.FirstName + '' '' + i.LastName AS ''ReportedBy''
from CaseFiles C 
join InvestigatorCaseFileAccess IA on C.FileID=IA.CaseFileID 
JOIN CaseFileTimeBills tb on C.FileID=tb.FileID
JOIN CaseFileExpenses e on C.FileID=e.FileID

left join Element07a_Persons i on i.PersonID = c.PersonID
LEFT OUTER JOIN CaseFileInvestigationTypes CFIT ON C.InvestigationTypeID = CFIT.InvestigationTypeID 
LEFT OUTER JOIN Investigators INV ON C.InvestigatorID = INV.InvestigatorID
where Deleted=0 and IA.InvestigatorID=' + CONVERT (nvarchar(max),@InvestigatorID) + ' and IA.AllowAccess = ''True'' 
and tb.InvNumber IS NULL OR e.InvNumber IS NULL '
END

“tb.Invnumber IS NULL或e.Invnumber IS NULL”没有返回它应该返回的行。当我运行查询时它返回5行,但我知道它假设返回的多于5。

1 个答案:

答案 0 :(得分:2)

我相信添加parens将解决您的问题:

Select @Command = 
'select DISTINCT C.FileID, C.FileName, convert(nvarchar,C.DateReported,111) AS ''DateReported'', 
C.FileDetailsPlainText, CFIT.Level3 as ''InvestigationType'', INV.FName + '' '' + INV.LName AS ''Name'', 
convert(nvarchar,DD,111) AS ''DD'', i.FirstName + '' '' + i.LastName AS ''ReportedBy''
from CaseFiles C 
join InvestigatorCaseFileAccess IA on C.FileID=IA.CaseFileID 
JOIN CaseFileTimeBills tb on C.FileID=tb.FileID
JOIN CaseFileExpenses e on C.FileID=e.FileID

left join Element07a_Persons i on i.PersonID = c.PersonID
LEFT OUTER JOIN CaseFileInvestigationTypes CFIT ON C.InvestigationTypeID = CFIT.InvestigationTypeID 
LEFT OUTER JOIN Investigators INV ON C.InvestigatorID = INV.InvestigatorID
where Deleted=0 and IA.InvestigatorID=' + CONVERT (nvarchar(max),@InvestigatorID) + ' and IA.AllowAccess = ''True'' 
and (tb.InvNumber IS NULL OR e.InvNumber IS NULL)'
END