Skip to content

Commit 3eed5d5

Browse files
committed
Improve documentation
1 parent 5451ae8 commit 3eed5d5

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

doc/sql.extensions/README.create_table_as_query.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ the same number of columns as the query result.
1616
If no column name list is specified, column names are taken from the query output names. Unnamed expressions must
1717
be explicitly aliased.
1818

19-
`WITH DATA` is the default and inserts the query result into the newly created table. `WITH NO DATA` creates only
20-
the table definition.
19+
`WITH DATA` is the default and inserts the query result into the newly created table in the same transaction.
20+
`WITH NO DATA` creates only the table definition.
2121

2222
For global and local temporary tables, normal temporary-table data lifetime rules apply. Package temporary tables
2323
do not support this syntax.
@@ -53,3 +53,56 @@ CREATE LOCAL TEMPORARY TABLE tx_work AS
5353
FROM employee
5454
WITH NO DATA;
5555
```
56+
57+
## ISQL behavior
58+
59+
ISQL in AUTODDL mode (the default) uses separate transactions for DDL and DML statements.
60+
When `CREATE TABLE ... AS <query>` is executed with `WITH DATA`, the table creation and data population occur in the
61+
DDL transaction.
62+
63+
For regular tables, the inserted rows are not visible to the DML transaction until the DDL transaction is committed.
64+
For temporary tables, this behavior is even more surprising because the rows belong to the DDL transaction and are
65+
therefore not visible to the DML transaction at all.
66+
67+
For example:
68+
69+
```sql
70+
SQL> CREATE TABLE T1 AS SELECT 1 A FROM RDB$DATABASE;
71+
SQL> SELECT * FROM T1;
72+
73+
SQL> COMMIT;
74+
SQL> SELECT * FROM T1;
75+
76+
A
77+
============
78+
1
79+
```
80+
81+
With a temporary table:
82+
83+
```sql
84+
SQL> CREATE GLOBAL TEMPORARY TABLE T1 AS SELECT 1 A FROM RDB$DATABASE;
85+
SQL> SELECT * FROM T1;
86+
87+
SQL> COMMIT;
88+
SQL> SELECT * FROM T1;
89+
```
90+
91+
The table exists, but no rows are returned because the data was inserted in the DDL transaction and is not visible to
92+
the DML transaction.
93+
94+
To avoid this behavior, disable AUTODDL before executing the statement:
95+
96+
```sql
97+
SQL> SET AUTODDL OFF;
98+
99+
SQL> CREATE GLOBAL TEMPORARY TABLE T1 AS SELECT 1 A FROM RDB$DATABASE;
100+
SQL> SELECT * FROM T1;
101+
102+
A
103+
============
104+
1
105+
```
106+
107+
When AUTODDL is disabled, both the table creation and data population occur in the current transaction, making the
108+
inserted rows immediately visible.

0 commit comments

Comments
 (0)