您可以使用内联SQL创建外部联接吗

时间:2019-09-26 14:53:47

标签: tsql outer-join

我需要使用内联sql创建外部联接。我无法弄清楚。

select el.emp_number,el.first_name,el.last_name,e.job_desc_abbr as item_description,
el.level_one,el.level_one_firstname,el.level_one_lastname,
el.level_two,
el.level_two_firstname,el.level_two_lastname,
el.level_three,el.level_three_firstname,el.level_three_lastname
 from emp_level el,emp_information_test e
 right join (select empid,job_desc_abbr from emp_information_test) level1 on level1.empid = el.level_one   <--- Error
  where el.emp_number = e.empid
  and  e.deptno <> '999'
  and e.empid in ('547787','253010')
  

Msg 4104,第16级,状态1,第7行
   无法绑定多部分标识符“ el.level_one”。

我不确定是否可行。我需要语法方面的帮助。谢谢

1 个答案:

答案 0 :(得分:0)

正如我在评论中所写,您的问题是隐式和显式联接之间的混合。
由于显式联接已经成为ANSI-SQL的一部分已有30多年了,因此,确实不再需要使用隐式联接。

这是您的select语句的有效版本:

SELECT   el.emp_number
        ,el.first_name
        ,el.last_name
        ,e.job_desc_abbr AS item_description
        ,el.level_one
        ,el.level_one_firstname
        ,el.level_one_lastname
        ,el.level_two
        ,el.level_two_firstname
        ,el.level_two_lastname
        ,el.level_three
        ,el.level_three_firstname
        ,el.level_three_lastname
FROM emp_level AS el
JOIN emp_information_test AS e
    ON el.emp_number = e.empid
RIGHT JOIN (
    SELECT   empid
            ,job_desc_abbr 
    FROM emp_information_test
) AS level1 
     ON level1.empid = el.level_one   
WHERE e.deptno <> '999'
AND e.empid IN ('547787','253010')