|
| 1 | += Table Value Constructor |
| 2 | +:database-version: 23.2.0 |
| 3 | +:database-category: sql |
| 4 | + |
| 5 | +The Table Value Constructor can be used to generate multiple |
| 6 | +table values (e.g. rows) in a single invocation. |
| 7 | + |
| 8 | +*This feature is part of the ISO SQL:1992 standard.* |
| 9 | + |
| 10 | + |
| 11 | +[source,sql] |
| 12 | +[subs="verbatim"] |
| 13 | +---- |
| 14 | +-- Table to demonstrate the table value constructor |
| 15 | +CREATE TABLE bookings |
| 16 | +( |
| 17 | + id NUMBER, |
| 18 | + location VARCHAR2(25), |
| 19 | + type VARCHAR2(25) |
| 20 | +); |
| 21 | +
|
| 22 | +-- Insert multiple rows with a single insert statement |
| 23 | +INSERT INTO bookings |
| 24 | + VALUES (12113, 'Vienna', 'Family'), |
| 25 | + (62361, 'San Francisco', 'Business'), |
| 26 | + (38172, 'Berlin', 'Leisure'); |
| 27 | +
|
| 28 | +-- Retrieve newly inserted rows |
| 29 | +SELECT * FROM bookings; |
| 30 | +
|
| 31 | +-- Generate multiple rows using the table value constructor |
| 32 | +SELECT employee_id, first_name |
| 33 | + FROM ( |
| 34 | + VALUES (1,'Scott'), |
| 35 | + (2,'James'), |
| 36 | + (3,'John') |
| 37 | + ) employees (employee_id, first_name); |
| 38 | +---- |
| 39 | + |
| 40 | +.Result |
| 41 | +[source,sql] |
| 42 | +[subs="verbatim"] |
| 43 | +---- |
| 44 | +SQL> -- Table to demonstrate the table value constructor |
| 45 | +SQL> CREATE TABLE bookings |
| 46 | + 2 ( |
| 47 | + 3 id NUMBER, |
| 48 | + 4 location VARCHAR2(25), |
| 49 | + 5 type VARCHAR2(25) |
| 50 | + 6 ); |
| 51 | +
|
| 52 | +Table BOOKINGS created. |
| 53 | +
|
| 54 | +SQL> -- Insert multiple rows with a single insert statement |
| 55 | +SQL> INSERT INTO bookings |
| 56 | + 2 VALUES (12113, 'Vienna', 'Family'), |
| 57 | + 3 (62361, 'San Francisco', 'Business'), |
| 58 | + 4 (38172, 'Berlin', 'Leisure'); |
| 59 | +
|
| 60 | +3 rows inserted. |
| 61 | +
|
| 62 | +SQL> -- Retrieve newly inserted rows |
| 63 | +SQL> SELECT * FROM bookings; |
| 64 | +
|
| 65 | + ID LOCATION TYPE |
| 66 | +-------- ---------------- -------- |
| 67 | + 12113 Vienna Family |
| 68 | + 62361 San Francisco Business |
| 69 | + 38172 Berlin Leisure |
| 70 | +
|
| 71 | +SQL> -- Generate multiple rows using the table value constructor |
| 72 | +SQL> SELECT employee_id, first_name |
| 73 | + 2 FROM ( |
| 74 | + 3 VALUES (1,'Scott'), |
| 75 | + 4 (2,'James'), |
| 76 | + 5 (3,'John') |
| 77 | + 6 ) employees (employee_id, first_name); |
| 78 | +
|
| 79 | + EMPLOYEE_ID FIRST_NAME |
| 80 | +-------------- ---------- |
| 81 | + 1 Scott |
| 82 | + 2 James |
| 83 | + 3 John |
| 84 | +---- |
| 85 | + |
| 86 | +== Benefits |
| 87 | + |
| 88 | +The table value constructor enables users to insert multiple rows within a |
| 89 | +single `INSERT` operation. This avoids unnecessary additional `INSERT` |
| 90 | +executions and roundtrips from and to the client for inserting multiple rows. |
| 91 | + |
| 92 | +Using this feature can make SQL scripts more concise and readable. |
| 93 | + |
| 94 | +Using this feature can save time when writing `INSERT` statements manually. |
| 95 | + |
| 96 | +The table value constructor provides a convenient and concise way to |
| 97 | +generate multiple rows with values. |
| 98 | + |
| 99 | +== Further information |
| 100 | + |
| 101 | +* Introduced: xref:versions:{database-version}/index.adoc[] |
| 102 | +* Availability: All Offerings |
| 103 | +* link:https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6[Multi-row `INSERT`] and link:https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6[`VALUES` row generation] documentation |
| 104 | +* link:https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6[Multi-row `INSERT`] and link:https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6[`VALUES` row generation] example |
0 commit comments