// dialect comparison

Insert several rows in one statement

One round trip, many rows: the VALUES list that is a one-liner everywhere except older Oracle.

Filed under Inserting & updating

The verdict

SQL Server, PostgreSQL, MySQL, SQLite and Oracle 23ai share identical syntax. Older Oracle needs INSERT … SELECT … FROM dual UNION ALL, which is wordier but does the same job in one statement.

Where they differ

Listing several row tuples in one VALUES clause is the normal way to batch small inserts. Oracle accepted it only from 23ai; earlier versions need INSERT ALL or a SELECT-based insert. Each engine also caps the statement in its own way, covered in bulk-insert-limits.

How each engine writes it

Oracle

INSERT INTO customers (email)
SELECT '[email protected]' FROM dual UNION ALL
SELECT '[email protected]' FROM dual UNION ALL
SELECT '[email protected]' FROM dual;

Works on every version. 23ai also accepts the multi-row VALUES list used by the other four. INSERT ALL is the other classic form, but it generates one identity or sequence value per source row, so three branches into a table with an identity key collide.

Questions that come up

How many rows should one INSERT carry?

A few hundred to a few thousand is comfortable on every engine, and SQL Server stops at 1,000. Beyond that, statement size limits and parse time make a bulk loader or INSERT … SELECT from a staging table the better tool.

What about Oracle's INSERT ALL?

INSERT ALL INTO t VALUES (…) INTO t VALUES (…) SELECT * FROM dual is a multi-table insert driven by a subquery: each INTO branch runs once per row the subquery returns, and dual has one row. Because identity and NEXTVAL values are generated once per source row, every branch gets the same id, and inserts into an identity-keyed table fail with ORA-00001.

Is a multi-row insert all-or-nothing?

Yes on transactional tables: it is one statement, so a failing row (say, a duplicate key) rolls back the whole insert. MySQL's INSERT IGNORE turns such errors into warnings and skips the bad rows instead.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.