SQL Are these the same?
Are these the same?
INNER JOIN dbo.ReportingLevels rl ON e.ei_CompanyID = rl.rl_CompanyId
AND e.ei_Level = rl.rl_index
AND EXISTS (SELECT * FROM @ReportingLevelId rlid
WHERE rlid.[reportLevelName] = rl.[rl_name])
AND EXISTS (SELECT * FROM @ReportingLevelId rlid
WHERE rlid.[companyid] = rl.[rl_CompanyId])
And here is the second.
INN开发者_高级运维ER JOIN dbo.ReportingLevels rl ON e.ei_CompanyID = rl.rl_CompanyId
AND e.ei_Level = rl.rl_index
AND EXISTS (SELECT * FROM @ReportingLevelId rlid
WHERE rlid.[reportLevelName] = rl.[rl_name]
AND rlid.[companyid] = rl.[rl_CompanyId]))
I am thinking these are the same, but I got a weird result once when I used a similar variation of this.
No, they are not. The first would select a row if rl_name and rl_CompanyId were present in table @ReportingLevelId, but not necessarily in the same row. The second requires those values to be present in the same row.
There is a problem on your secound sql, you are using AND instead of OR.You can see the correct above.
INNER JOIN dbo.ReportingLevels rl ON e.ei_CompanyID = rl.rl_CompanyId AND e.ei_Level = rl.rl_index
AND EXISTS (SELECT * FROM @ReportingLevelId rlid WHERE rlid.[reportLevelName] = rl.[rl_name] OR rlid.[companyid] = rl.[rl_CompanyId]))
精彩评论