3

I'm updating queries that were using the old construction for outer joins (=* and *=). I have 3 tables that I need to include in an outer join.

The original query is:

SELECT  s.SkillID ,
        NULL AS Signature ,
        NULL AS DPL ,
        CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
             THEN p.ScaleTo - p.ScaleFrom + 1
             ELSE ISNULL(ds.DPL, dg.DPL)
        END AS DefaultDPL
FROM    tbJobs j ,
        tbSkills s 
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
        tbPerfScales p ,
        tbDPLs ds ,
        tbDPLs dg
WHERE   j.JobID = 866
        AND ( ds.LevelID=*j.LevelID
              AND ds.IDType = 1
              AND ds.GroupOrSkillID=*s.SkillID
            )
        AND ( dg.LevelID=*j.LevelID
              AND dg.IDType = 0
              AND dg.GroupOrSkillID=*sg.SkillGroupID
            )
        AND ( ( s.PerfScaleID IS NOT NULL
                AND p.PerfScaleID = s.PerfScaleID
              )
              OR ( s.PerfScaleID IS NULL
                   AND p.PerfScaleID = sg.PerfScaleID
                 )
            )

I'm doing:

SELECT  s.SkillID ,
        NULL AS Signature ,
        NULL AS DPL ,
        CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
             THEN p.ScaleTo - p.ScaleFrom + 1
             ELSE ISNULL(ds.DPL, dg.DPL)
        END AS DefaultDPL
FROM    tbPerfScales p ,
        tbSkills s
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
        tbJobs j
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID
WHERE   j.JobID = 866
        AND ds.IDType = 1
        AND dg.IDType = 0
        AND ( ( s.PerfScaleID IS NOT NULL
                AND p.PerfScaleID = s.PerfScaleID
              )
              OR ( s.PerfScaleID IS NULL
                   AND p.PerfScaleID = sg.PerfScaleID
                 )
            )

For some reason I'm getting the error:

The multi-part identifier "s.SkillID" could not be bound.

And I know is in this part:

    tbJobs j
    LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                 AND s.SkillID = ds.GroupOrSkillID

I'm not sure what I'm doing wrong.

Thanks any help. Jose

2
  • Yopu may find that you have diffeernt results than in the past wehn you do this because the outer joins in the implicit syntax have been producing incorrect results in many cases even as far back as SQL server 2000. So every query you are fixing was potentially incorrect to begin with. And you may very well get bad results mixing implict and explicit syntax, you must convert all joins to have correct results.
    – HLGEM
    Oct 2, 2012 at 18:53
  • That might be true for some cases and that's why I'm running lots of tests for them to ensure they produce accurate results. Thanks for the tip.
    – JRGuay
    Oct 4, 2012 at 17:15

2 Answers 2

6

There is a comma after the statement

INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,

Which is because you have tbJobs after it, when it should be in the other tables. I would recommend using CROSS JOIN instead of just having multiple tables in the FROM clause as it will be more clear.

Here is the base rewrite of your query that should work, though you should be able to easily get rid of most of the cross joins.

SELECT  s.SkillID ,
        NULL AS Signature ,
        NULL AS DPL ,
        CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
             THEN p.ScaleTo - p.ScaleFrom + 1
             ELSE ISNULL(ds.DPL, dg.DPL)
        END AS DefaultDPL
FROM    tbPerfScales p 
        CROSS JOIN tbSkills s
        CROSS JOIN tbJobs j
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID
WHERE   j.JobID = 866
        AND ds.IDType = 1
        AND dg.IDType = 0
        AND ( ( s.PerfScaleID IS NOT NULL
                AND p.PerfScaleID = s.PerfScaleID
              )
              OR ( s.PerfScaleID IS NULL
                   AND p.PerfScaleID = sg.PerfScaleID
                 )
            )
2
  • Thanks Darren. I have the comma because as I said I'm rewriting the original query and basically all I want (need) is to eliminate the old outer join format. The comma there is to separate another part of the joins, do you think that could be the issue? I don't have any column/way to join the previous 3 tables to the later 2 ones. :(
    – JRGuay
    Oct 2, 2012 at 16:11
  • yeah, there shouldn't be a comma between joins, so just move that tbJobs up. I updated my question w/ cross join usage. Oct 2, 2012 at 17:32
3

You cannot mix the different SQL syntax. If you are going with ANSI-92 you need to correct all the joins, not just the outer joins. Consistency is key.

The comma is the incorrect syntax now. You need to add a join for each table, or pull them out into cross applies per Darren's answer. This is the issue:

FROM    tbPerfScales p ,
        tbSkills s
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
        tbJobs j
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID

it needs to be along the lines of:

  FROM    tbPerfScales p 
     INNER JOIN tbSkills s ON  p.<field> = s.<field>
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID
        INNER JOIN tbJobs j on j.<field> = <table p? sg? not sure>.<field>
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID
1
  • It actually makes a lot of sense, only I don't have columns to join the other tables. See the original query? Not sure how to pull that one out.
    – JRGuay
    Oct 2, 2012 at 17:00

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.