1

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]
4
  • if you want the entries that are deleted = 1, then why are you explicitly asking for isdeleted = 0?
    – Ramy
    Nov 23, 2010 at 22:12
  • I really wish SQL Server would not accept what you posted valid syntax - defeats the purpose of ANSI-92 readability :/ Also, DO NOT post code with formatted with tab (\t), please.
    – OMG Ponies
    Nov 23, 2010 at 22:13
  • Is there a need to bring back all the data in one query? Could you bring it back in pieces?
    – DerekH
    Nov 23, 2010 at 22:17
  • @army you misread what I said. I said that I want to pull entries that aren't deleted. Nov 23, 2010 at 22:31

3 Answers 3

3

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.

1
  • funny that was my first try ... i think i may have done it in haste and inadvertently mixed a column or something cause that worked perfectly! =D ty sire Nov 23, 2010 at 22:47
0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.