0

我有一个 sql 选择查询,它应该通过 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 OR e.Invnumber IS NULL”没有返回它应该返回的行。当我运行查询时它返回 5 行,但我知道它应该返回超过 5 行。

4

1 回答 1

2

我相信添加括号会解决您的问题:

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
于 2012-11-05T19:03:37.853 回答