Multirow inserts A SQL feature (since
SQL-92) is the use of
row value constructors to insert multiple rows at a time in a single SQL statement: INSERT INTO tablename (column-a, [column-b, ...]) VALUES ('value-1a', ['value-1b', ...]), ('value-2a', ['value-2b', ...]), ... This feature is supported by
IBM Db2,
SQL Server (since version 10.0 - i.e. 2008),
PostgreSQL (since version 8.2),
MySQL,
SQLite (since version 3.7.11) and
H2. Example (assuming that 'name' and 'number' are the only columns in the 'phone_book' table): INSERT INTO phone_book VALUES ('John Doe', '555-1212'), ('Peter Doe', '555-2323'); which may be seen as a shorthand for the two statements INSERT INTO phone_book VALUES ('John Doe', '555-1212'); INSERT INTO phone_book VALUES ('Peter Doe', '555-2323'); Note that the two separate statements may have different semantics (especially with respect to statement
triggers) and may not provide the same performance as a single multi-row insert. To insert multiple rows in MS SQL you can use such a construction: INSERT INTO phone_book SELECT 'John Doe', '555-1212' UNION ALL SELECT 'Peter Doe', '555-2323'; Note that this is not a valid SQL statement according to the SQL standard (
SQL:2003) due to the incomplete subselect clause. To do the same in Oracle use the
DUAL table, which always consists of a single row only: INSERT INTO phone_book SELECT 'John Doe', '555-1212' FROM DUAL UNION ALL SELECT 'Peter Doe','555-2323' FROM DUAL A standard-conforming implementation of this logic shows the following example, or as shown above: INSERT INTO phone_book SELECT 'John Doe', '555-1212' FROM LATERAL ( VALUES (1) ) AS t(c) UNION ALL SELECT 'Peter Doe','555-2323' FROM LATERAL ( VALUES (1) ) AS t(c) Oracle PL/SQL supports the statement, where multiple insert statements are terminated by a : INSERT ALL INTO phone_book VALUES ('John Doe', '555-1212') INTO phone_book VALUES ('Peter Doe', '555-2323') SELECT * FROM DUAL; In
Firebird inserting multiple rows can be achieved like this: INSERT INTO phone_book (name, number) SELECT 'John Doe', '555-1212' FROM RDB$DATABASE UNION ALL SELECT 'Peter Doe', '555-2323' FROM RDB$DATABASE; Firebird, however, restricts the number of rows than can be inserted in this way, since there is a limit to the number of contexts that can be used in a single query.
Copying rows from other tables An statement can also be used to retrieve data from other tables, modify it if necessary and insert it directly into the table. All this is done in a single SQL statement that does not involve any intermediary processing in the client application. A subselect is used instead of the clause. The subselect can contain joins, function calls, and it can even query the same table into which the data is inserted. Logically, the select is evaluated before the actual insert operation is started. An example is given below. INSERT INTO phone_book2 SELECT * FROM phone_book WHERE name IN ('John Doe', 'Peter Doe') A variation is needed when some of the data from the source table is being inserted into the new table, but not the whole record. (Or when the tables'
schemas are not the same.) INSERT INTO phone_book2 (name, number) SELECT name, number FROM phone_book WHERE name IN ('John Doe', 'Peter Doe') The statement produces a (temporary) table, and the schema of that temporary table must match with the schema of the table where the data is inserted into. ==Default Values==