Monday, February 7, 2011

Be Careful When Apply Restrictions on Outer Joins

This query doesn't work as expected.
select

rd.System_Id as BizStoryID ,0 as TechStoryID ,0 as TaskID from


#UpdatedWorkItemRawData rdleft join #ItemHierarchyPrep ihon rd.System_Id = ih.BizStoryIDand rd.System_WorkItemType = 'eScrum Product Backlog Item'and rd.TeamProjectSK = @ProductBacklogProjectNodeSKwhere ih.BizStoryID is null

To get what I need, the following query is needed.
 

select rd.System_Id as BizStoryID ,0 as TechStoryID ,0 as TaskID from

#UpdatedWorkItemRawData rdleft join #ItemHierarchyPrep ihon rd.System_Id = ih.BizStoryIDwhere rd.System_WorkItemType = 'eScrum Product Backlog Item'
and rd.TeamProjectSK = @ProductBacklogProjectNodeSKand ih.BizStoryID is null