SELECT from nothing?
Solution 1:
It's not consistent across vendors - Oracle, MySQL, and DB2 support dual:
SELECT 'Hello world'
FROM DUAL
...while SQL Server, PostgreSQL, and SQLite don't require the FROM DUAL
:
SELECT 'Hello world'
MySQL does support both ways.
Solution 2:
In Oracle:
SELECT 'Hello world' FROM dual
Dual equivalent in SQL Server:
SELECT 'Hello world'
Solution 3:
Try this.
Single:
SELECT * FROM (VALUES ('Hello world')) t1 (col1) WHERE 1 = 1
Multi:
SELECT * FROM (VALUES ('Hello world'),('Hello world'),('Hello world')) t1 (col1) WHERE 1 = 1
more detail here : http://modern-sql.com/use-case/select-without-from
Solution 4:
Here is the most complete list of database support of dual from https://blog.jooq.org/tag/dual-table/:
In many other RDBMS, there is no need for dummy tables, as you can issue statements like these:
SELECT 1; SELECT 1 + 1; SELECT SQRT(2);
These are the RDBMS, where the above is generally possible:
- H2
- MySQL
- Ingres
- Postgres
- SQLite
- SQL Server
- Sybase ASE
In other RDBMS, dummy tables are required, like in Oracle. Hence, you’ll need to write things like these:
SELECT 1 FROM DUAL; SELECT 1 + 1 FROM DUAL; SELECT SQRT(2) FROM DUAL;
These are the RDBMS and their respective dummy tables:
- DB2: SYSIBM.DUAL
- Derby: SYSIBM.SYSDUMMY1
- H2: Optionally supports DUAL
- HSQLDB: INFORMATION_SCHEMA.SYSTEM_USERS
- MySQL: Optionally supports DUAL
- Oracle: DUAL
- Sybase SQL Anywhere: SYS.DUMMY
Ingres has no DUAL, but would actually need it as in Ingres you cannot have a WHERE, GROUP BY or HAVING clause without a FROM clause.