开发者

My query loses data as the result of an incomplete where clause

I know that the issue is in my WHERE clause but I don't have a good solution to correct the problem.

if a entry in section 2 is not marked as deleted and is linked to a section 3 entry which is marked as deleted then I lose the entry altogether.

Any help would be appreciated.

p.s this same thing occurs from 3 to 4 as well the table structure cannot be altered unfortunately ... before my time.

Table       FK         PK         bit
------------------------------------------
Section1 |  JobID   | Sect1ID | isDeleted
Section2 |  Sect1ID | Sect2ID | isDeleted
Section3 |  Sect2ID | Sect3ID | isDeleted
Section4 |  Sect3ID | Sect4ID | isDeleted

Stored procedure:

    @JobId int
    AS
SET NOCOUNT ON

SELECT J.Section1,
    J.Section2,
    J.Section3,
    J.Section4,
    S1.Id AS Section1Id,
    S1.[Order] + 1 AS S1Order,
    S1.Text AS S1Text,
    S2.Id 开发者_运维问答AS Section2Id,
    S2.Text AS S2Text,
    S2.[Order] AS S2Order,
    S3.Id AS Section3Id,
    S3.Text AS S3Text,
    S3.[Order] + 1 AS S3Order,
    S4.Id AS Section4Id,
    S4.Text AS S4Text,
    S4.[Order] AS S4Order
       FROM JT_Jobs J 
       JOIN JT_Section1 S1 ON J.JobId = S1.JobId
  LEFT JOIN JT_Section2 S2 ON S1.Id = S2.Section1Id
  LEFT JOIN JT_Section3 S3 ON S2.Id = S3.Section2Id
  LEFT JOIN JT_Section4 S4 ON S3.Id = S4.Section3Id
      WHERE J.JobId = @JobId 
        AND S1.IsDeleted = 0
        AND (s2.IsDeleted is null or s2.IsDeleted = 0) 
        AND (s3.IsDeleted is null or s3.IsDeleted = 0) 
        AND (s4.IsDeleted is null or s4.IsDeleted = 0)
   ORDER BY S1.[Order], S2.[Order], S3.[Order], S4.[Order]


Put the join conditions in the join clause and not in the where clause

  From JT_Jobs J
     Join JT_Section1 S1 
         On S1.JobId = J.JobId
            And S1.IsDeleted = 0
     Left Join JT_Section2 S2
         On S2.Section1Id=S1.Id 
            And (s2.IsDeleted is null or s2.IsDeleted = 0)  
     Left Join JT_Section3 S3 
         On S3.Section2Id = S2.iD
            And (s3.IsDeleted is null or s3.IsDeleted = 0)  
     Left Join JT_Section4 S4
         ON S4.Section3Id = S3.Id
            And (s4.IsDeleted is null or s4.IsDeleted = 0)    
  Where J.JobId = @JobId  

.... to explain, predicates (boolean conditions) in a where clause are only evaluated after all the joins have been processed.

Joins are processed (logically) by examining both sides of the join, adding records into the resultset where all the join conditions are true, and then, if it's an outer join, adding back the records from the outer side that did not have a matching record on the other side. So the join conditions are only applied to the inner side, and to the records on the outer side that match a record on the inner side.

So putting a condition in the where clause causes it to be applied to the last intermediate resultset, after all joins have been processed. At this point all those outer records have been added back in, and for a condition which affects the outer side of an outer join, it will eliminate all those records that were added back in during the second half of that outer join, effectively turniug it back into an inner join.


Add the sN.IsDeleted = 0 to the LEFT JOIN clauses, not in the WHERE clause.


When you reference fields in the where clause (except where ID is null) for tables on the right side of a left join, you turn it into an inner join. You must put those conditions into the join criteria.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜