The above query will generate all the pairings between two players. The same can be written in pre-ANSI SQL-92 with comma-separated table lists:
SELECT p1.name player1, p2.name player2 FROM player p1 CROSS JOIN player p2
The first syntax, however, is more powerful and more expressive as it can be used in nested JOIN expressions and it shows your intent more clearly. A nice example was given here in a previous blog post.
SELECT p1.name player1, p2.name player2 FROM player p1, player p2
How to work around a missing CROSS JOINUsually, missing support for
CROSS JOINcan be emulated trivially using an
INNER JOINwith a
TRUEpredicate as such:
This is what jOOQ does for the Sybase Adaptive Server Enterprise database. But this doesn’t work for MS Access, because the
SELECT p1.name player1, p2.name player2 FROM player p1 JOIN player p2 ON 1 = 1
JOINoperation there explicitly requires column references from either table on either side. The documentation reads:
Syntax:This is quite a bummer from many points of view, not only forFROM table1 INNER JOIN table2 ON table1.field1 compopr table2.field2
CROSS JOINemulation. Given that any ANSI-92
JOINsyntax can be transformed into an ANSI-86 join expression (table list in the
FROMclause and all predicates in the
WHEREclause), it is also a bit surprising. A simple workaround that seems to work for some use-cases is to take any numeric column from either table, and multiply it by zero:
But if the database itself is already this quirky, I suspect that it might not be able to optimise the above SQL.
SELECT p1.name player1, p2.name player2 FROM player p1 JOIN player p2 ON p1.id * 0 = p2.id * 0
In short…MS Access does not support
CROSS JOIN. For the time being, try to work around it using comma-separated table lists, while we work out more sophisticated SQL transformation in jOOQ.
5 thoughts on “No CROSS JOIN in MS Access”
Maybe ucanaccess is a hack but it’s currently the one open-source solution because it runs pretty fine on non-windows os Operative Systems and because the JDBC-ODBC bridge will be removed in Java 8(and, you know, I like to win easy).
It supports both access and standard SQL(e.g., both CROSS JOIN and the terrible square brackets).
It’s a very young project but it’s quickly improving in the latest versions (e.g., with the 2.x.x you can access to the access system tables… ops another pun intended). So, keep it in mind for the next JOOQ versions!;)
Thanks for chiming in! Yes, we actually have quite a bit of respect for ucanaccess. Even if it’s a hack, it’s a quite cunning one, and I believe it has a good future!
In the short run, we didn’t want to base jOOQ’s Access support on an external dependency as we’re estimating that MS Access users might not jump on the JDK 8 in 2014 or 2015 yet, and even if they do, there are also commercial JDBC-ODBC bridges to replace the Sun one.
But we’ll certainly keep this option in mind and we’ll happily contribute to your project, should we integrate in the future.
Actually, can I access (haha, running-pun) relationship information through ucanaccess? That doesn’t seem to be reliable through MS Access / JDBC-ODBC
Yes, of course, you can do it in two different ways:
This is the most reliable way to get metadata about relationships through ucanaccess.
and executing queries like this:
Thanks, hope to hear from you soon, Marco
Great! I guess I’d have to resort to getCrossReference, as jOOQ does not control connection parameters. It would be more user-friendly, as it would work out of the box…