I’m currently reviewing a draft of Roger Troller’s updated PL/SQL and SQL Coding Guidelines version 3.0. One guideline recommends to use ANSI join syntax. The mentioned reasons are
ANSI join syntax does not have as many restrictions as the ORACLE join syntax has. Furthermore ANSI join syntax supports the full outer join. A third advantage of the ANSI join syntax is the separation of the join condition from the query filters.
While I read this I wondered which restrictions still exist for ORACLE join syntax nowadays and searched for “(+)” in the current Error Messages documentation (E49325-06) and found the following error messages:
In the 9.2 documentation (A96525-01) I found the following additional messages:
I’ve written SQL statements to produce the error message listed above on a 9.2.0.8 Oracle database and ran them on a 12.1.0.2 database as well to see which restrictions still exist for the outer join operator (+) as basis for my feedback to Roger. While writing the queries I thought this might be an interesting topic to blog about.
1 2 3 4 |
SELECT s.*, p.* FROM sh.sales s, sh.products p WHERE p.prod_id = s.prod_id(+) AND p.supplier_id(+) = s.channel_id; |
An ORA-01416 is thrown in 9.2.0.8 and in 12.1.0.2. You cannot formulate such a query using ANSI join. Doing something like that does not make sense. It is not a relevant restriction. But it is interesting to see that an ORA-01416 is thrown in Oracle 12.1.0.2, even if this error message is not documented anymore.
1 2 3 4 |
SELECT s.*, c.*, p.* FROM sh.sales s, sh.customers c, sh.products p WHERE p.prod_id = s.prod_id(+) AND c.cust_id = s.cust_id(+); |
An ORA-01417 is thrown in 9.2.0.8 but not in 12.1.0.2.
1 2 3 |
SELECT s.*, p.* FROM sh.sales s, sh.products p WHERE p.prod_id(+) = s.prod_id(+); |
An ORA-01468 is thrown in 9.2.0.8 and in 12.1.0.2. You cannot formulate such a query using ANSI join. It could have been a way to formulate a full outer join, but something like that is not supported with Oracle join syntax. ORA-01468 is not documented in Oracle 12.1.0.2, but nonetheless this error is thrown. I do not consider this a relevant restriction for Oracle join-syntax.
1 2 3 4 |
SELECT s.*, p.* FROM sh.sales s, sh.products p WHERE p.prod_id(+) = s.prod_id AND p.prod_category(+) IN ('Boys', 'Girls'); |
An ORA-01719 is thrown in 9.2.0.8 but not in 12.1.0.2.
1 2 3 4 |
SELECT s.* FROM sh.sales s WHERE s.time_id(+) = (SELECT MAX(t.time_id) FROM sh.times t); |
An ORA-01799 is thrown in 9.2.0.8 and in 12.1.0.2. You cannot formulate such a query using ANSI join. Of course you may rewrite this to a valid Oracle join or ANSI join query. Here’s an example:
1 2 3 4 5 |
SELECT s.*, t.max_time_id FROM sh.sales s, (SELECT MAX(t.time_id) AS max_time_id FROM sh.times t) t WHERE s.time_id(+) = t.max_time_id; |
Because the restriction applies to ANSI join as well, I do not consider this a relevant restriction for Oracle join syntax.
1 2 3 4 5 |
SELECT s.*, c.*, p.* FROM sh.sales s, sh.customers c JOIN sh.products p ON (p.prod_id = s.prod_id) WHERE c.cust_id = s.cust_id(+); |
An ORA-25156 is thrown in 9.2.0.8 and in 12.1.0.2. This is not really a restriction for Oracle join syntax. The grammar simply does not support to mix join syntax variants.
1 2 3 4 5 6 7 8 |
SELECT lpad(' ', (LEVEL - 1) * 3) || to_char(e.empno) || ' ' || e.ename(+) || ' ' || d.dname AS emp_name FROM scott.emp e, scott.dept d WHERE e.deptno(+) = d.deptno CONNECT BY PRIOR e.empno(+) = e.mgr START WITH e.ename(+) = 'KING' ORDER BY rownum, e.empno(+); |
An ORA-30563 is thrown in 9.2.0.8 and 12.1.0.2. Interesting is that if you remove the (+) on the highlighted line 2 the query works on 9.2.0.8 but not on 12.1.0.2. Using the (+) in a CONNECT BY clause, START WITH clause, or ORDER BY clause does not make sense. It is not possible using ANSI-join as well. The important part is the join itself on line 5 and this is working in conjunction with a CONNECT BY. Therefore I do consider this an irrelevant restriction for the Oracle join syntax.
The results of the example relevant statements are summarized in the following table.
Error message by test SQL | Relevant outer join restriction? | Result in 9.2.0.8 | Result in 12.1.0.2 |
---|---|---|---|
ORA-01416 two tables cannot be outer-joined to each other | No | Error | Error |
ORA-01417: a table may be outer joined to at most one other table | Yes | Error | OK |
ORA-01468 a predicate may reference only one outer-joined table | No | Error | Error |
ORA-01719: outer join operator (+) not allowed in operand of OR or IN | Yes | Error | OK |
ORA-01799: a column may not be outer-joined to a subquery | No | Error | Error |
ORA-25156: old style outer join (+) cannot be used with ANSI joins | No | Error | Error |
ORA-30563: outer join operator (+) is not allowed here | No | Error | Error |
6 Comments
Are you talking about 12.1.0.2 as used in your examples or about 12.2 which was released in March 2017?
Are there any differences regarding Outer Join restrictions between 12.1 and 12.2?
Hi Norbert,
At the time of writing no 12.2 was available… However, I just run the queries against an Oracle Database 12.2.0.1.170814. The results are identical to those against version 12.1.0.2.
HTH
Philipp
Is there any alternate recommendation in Oracle 12c to replace the functionality of using (+) in order by clause
Yes. Simply remove the
(+)
in the order by clause. For example:When Try to create the materialized view in oracle 12c system showing error outer join is not allowed here (ora-30563) any solution please help.
Regards
soumit
The full error message is:
IMHO the error message is good. I suggest to follow the instructions under “Action”.