This works as expected:
SELECT "Mike" AS FName
This fails with the error "Query input must contain at least one table or query":
SELECT "Mike" AS FName
UNION ALL
SELECT "John" AS FName
Is this just a quirk/limitation of the Jet/ACE database engine or am I missing something?
Here's a much simpler way to do it:
Important: TableWith1Row can EITHER be a table with literally 1 record (which you ignore anyway) OR it can be a table with any number of rows (must have AT LEAST 1 row) but you add a WHERE clause to ensure 1 row. This is a bit loosy-goosy but it's a quick way to make this work without creating more tables.
You didn't overlook anything. Access' database engine will allow a single row
SELECT
without aFROM
data source. But if you want toUNION
orUNION ALL
multiple rows, you must include aFROM
... even if you're not referencing any field from that data source.I created a table with one row and added a check constraint to guarantee it will always have one and only one row.
That
Dual
table is useful for queries such as this:Another approach I've seen is to use a
SELECT
statement withTOP 1
or aWHERE
clause which restricts the result set to a single row.Note check constraints were added with Jet 4 and are only available for statements executed from ADO.
CurrentProject.Connection.Execute strSql
works becauseCurrentProject.Connection
is an ADO object. If you try to execute the same statement with DAO (ieCurrentDb.Execute
or from the Access query designer), you will get a syntax error because DAO can't create check constraints.If you have access to some system tables, you can emulate a dual table this way:
Unfortunately, I'm not aware of any system tables that...
DUAL
or DB2'sSYSIBM.DUAL
So you'd write:
This is what is being implemented as a syntactic element in jOOQ, for instance.
When you have restricted read-only access to the database (i.e you cannot create new tables or access system resources), this could work:
anyTable is the first user table you find (I can hardly imagine a real-life database with no user table!).
WHERE 1=0 is supposed to return fast a count of 0, even on a large table (hopefully the Jet engine is smart enough to recognize such trivial condition).
If someone wants to use the Top 1 method, it would look like this:
The alias for the field has to be the same on both sides of the union, in this case "FName".
Put in any table name (you don't need to actually select a column from it).
This query gives me the 3 fiscal years I need for a dropdown. The fiscal year begins in July.