2009-10-15 70 views
1

我们已经在SQL Server以下查询:将SQL Server查询到Oracle外连接问题

SELECT b.columnB, 
     b.displayed_name AS displayName, 
     c.type_cd, 
     c.type_desc, 
     b.month_desc AS month 
FROM table1 a, table2 b, table3 c 
WHERE b.region_code *= a.columnA 
     AND c.program_type_cd *= a.program_type_cd 

其中,在Oracle中,得到了转化为:

SELECT b.columnB, 
     b.displayed_name displayName, 
     c.type_cd, 
     c.type_desc, 
     b.month_desc month 
FROM table1 a, 
     table2 b, 
     table3 c 
WHERE b.columnB = a.columnA(+) 
     AND c.type_cd = a.type_cd(+) 

不过,虽然在运行Oracle本我们得到一个错误

"a table must be outer joined to at most one other table" 

最好的方法来解决这个问题,并保持与SQL服务器相同的逻辑?

回答

1

尝试这一次,

SELECT b.columnB, 
     b.displayed_name displayName, 
     c.type_cd, 
     c.type_desc, 
     b.month_desc month 
FROM table1 a 
     LEFT JOIN table2 b ON b.columnB = a.columnA 
     LEFT JOIN tablec c ON c.type_cd = a.type_cd 
+0

如果* =和LEFT JOIN做同样的事情,那么我将def去LEFT JOIN。这就是为什么我讨厌编程中的注释。谢谢 – Omnipresent 2009-10-15 18:00:40

+0

其实我不认为这跟原来的查询是一样的:原来是从b到a的左连接,同时从c到a,这就是为什么Oracle抱怨的原因。重写的查询是从a到b和a到c的左连接,原始查询中原本应该= =而不是* =。 – 2009-10-17 06:27:10

+0

那么应该如何写入oracle呢? – Omnipresent 2009-10-20 02:31:14

0

为什么表1列出了OUTER JOIN,如果你不从它返回的数据?这似乎是你想要table1的是内部连接到表2,然后做这个一样2和3之间外:

SELECT b.columnB, 
     b.displayed_name displayName, 
     c.type_cd, 
     c.type_desc, 
     b.month_desc month 
FROM table1 a, 
     table2 b, 
     table3 c 
WHERE b.columnB = a.columnA 
     AND c.type_cd = a.type_cd(+) 

在另一方面,我recommond切换到ANSI连接(如在埃里克的例子中) - 他们更容易阅读,虽然在功能上,他们是相同的东西,并执行完全相同的方式。

+0

它被列出是因为原始查询很长并且有更多列。我缩短了这个具体问题。 – Omnipresent 2009-10-15 17:57:47