|
1 | 1 | --- |
2 | 2 | title: "INSERT (Transact-SQL) | Microsoft Docs" |
3 | 3 | ms.custom: "" |
4 | | -ms.date: "08/10/2017" |
| 4 | +ms.date: "04/21/2020" |
5 | 5 | ms.prod: sql |
6 | 6 | ms.prod_service: "database-engine, sql-database, sql-data-warehouse, pdw" |
7 | 7 | ms.reviewer: "" |
@@ -297,37 +297,43 @@ For information specific to inserting data into SQL graph tables, see [INSERT (S |
297 | 297 |
|
298 | 298 | ### Best Practices for Bulk Importing Data |
299 | 299 |
|
300 | | -#### Using INSERT INTO...SELECT to Bulk Import Data with Minimal Logging |
301 | | - You can use `INSERT INTO <target_table> SELECT <columns> FROM <source_table>` to efficiently transfer a large number of rows from one table, such as a staging table, to another table with minimal logging. Minimal logging can improve the performance of the statement and reduce the possibility of the operation filling the available transaction log space during the transaction. |
302 | | - |
303 | | - Minimal logging for this statement has the following requirements: |
| 300 | +#### Using INSERT INTO...SELECT to Bulk Import data with minimal logging and parallelism |
| 301 | +You can use `INSERT INTO <target_table> SELECT <columns> FROM <source_table>` to efficiently transfer a large number of rows from one table, such as a staging table, to another table with minimal logging. Minimal logging can improve the performance of the statement and reduce the possibility of the operation filling the available transaction log space during the transaction. |
304 | 302 |
|
| 303 | +Minimal logging for this statement has the following requirements: |
305 | 304 | - The recovery model of the database is set to simple or bulk-logged. |
306 | | - |
307 | | -- The target table is an empty or nonempty heap. |
308 | | - |
| 305 | +- The target table is an empty or non-empty heap. |
309 | 306 | - The target table is not used in replication. |
310 | | - |
311 | | -- The TABLOCK hint is specified for the target table. |
| 307 | +- The `TABLOCK` hint is specified for the target table. |
312 | 308 |
|
313 | 309 | Rows that are inserted into a heap as the result of an insert action in a MERGE statement may also be minimally logged. |
314 | 310 |
|
315 | | - Unlike the BULK INSERT statement, which holds a less restrictive Bulk Update lock, INSERT INTO...SELECT with the TABLOCK hint holds an exclusive (X) lock on the table. This means that you cannot insert rows using parallel insert operations. |
| 311 | +Unlike the `BULK INSERT` statement, which holds a less restrictive Bulk Update (BU) lock, `INSERT INTO … SELECT` with the `TABLOCK` hint holds an exclusive (X) lock on the table. This means that you cannot insert rows using multiple insert operations executing simultaneously. |
| 312 | + |
| 313 | +However, starting with [!INCLUDE[ssSQL15](../../includes/sssql15-md.md)] and database compatibility level 130, a single `INSERT INTO … SELECT` statement can be executed in parallel when inserting into heaps or clustered columnstore indexes (CCI). Parallel inserts are possible when using the `TABLOCK` hint. |
| 314 | + |
| 315 | +Parallelism for the statement above has the following requirements, which are similar to the requirements for minimal logging: |
| 316 | +- The target table is an empty or non-empty heap. |
| 317 | +- The target table has a clustered columnstore index (CCI) but no non-clustered indexes. |
| 318 | +- The target table cannot have an identity column specified. |
| 319 | +- The `TABLOCK` hint is specified for the target table. |
| 320 | + |
| 321 | +For scenarios where requirements for minimal logging and parallel insert are met, both improvements will work together to ensure maximum throughput of your data load operations. |
| 322 | + |
| 323 | +> [!NOTE] |
| 324 | +> Inserts into local temporary tables (identified by the # prefix) and global temporary tables (identified by ## prefixes) are also enabled for parallelism using the TABLOCK hint. |
316 | 325 | |
317 | | -#### Using OPENROWSET and BULK to Bulk Import Data |
| 326 | +#### Using OPENROWSET and BULK to Bulk Import data |
318 | 327 | The OPENROWSET function can accept the following table hints, which provide bulk-load optimizations with the INSERT statement: |
319 | 328 |
|
320 | | -- The TABLOCK hint can minimize the number of log records for the insert operation. The recovery model of the database must be set to simple or bulk-logged and the target table cannot be used in replication. For more information, see [Prerequisites for Minimal Logging in Bulk Import](../../relational-databases/import-export/prerequisites-for-minimal-logging-in-bulk-import.md). |
321 | | - |
322 | | -- The IGNORE_CONSTRAINTS hint can temporarily disable FOREIGN KEY and CHECK constraint checking. |
323 | | - |
324 | | -- The IGNORE_TRIGGERS hint can temporarily disable trigger execution. |
| 329 | +- The `TABLOCK` hint can minimize the number of log records for the insert operation. The recovery model of the database must be set to simple or bulk-logged and the target table cannot be used in replication. For more information, see [Prerequisites for Minimal Logging in Bulk Import](../../relational-databases/import-export/prerequisites-for-minimal-logging-in-bulk-import.md). |
| 330 | +- The `TABLOCK` hint can enable parallel insert operations. The target table is a heap or clustered columnstore index (CCI) with no non-clustered indexes, and the target table cannot have an identity column specified. |
| 331 | +- The `IGNORE_CONSTRAINTS` hint can temporarily disable FOREIGN KEY and CHECK constraint checking. |
| 332 | +- The `IGNORE_TRIGGERS` hint can temporarily disable trigger execution. |
| 333 | +- The `KEEPDEFAULTS` hint allows the insertion of a table column's default value, if any, instead of NULL when the data record lacks a value for the column. |
| 334 | +- The `KEEPIDENTITY` hint allows the identity values in the imported data file to be used for the identity column in the target table. |
325 | 335 |
|
326 | | -- The KEEPDEFAULTS hint allows the insertion of a table column's default value, if any, instead of NULL when the data record lacks a value for the column. |
327 | | - |
328 | | -- The KEEPIDENTITY hint allows the identity values in the imported data file to be used for the identity column in the target table. |
329 | | - |
330 | | -These optimizations are similar to those available with the BULK INSERT command. For more information, see [Table Hints (Transact-SQL)](../../t-sql/queries/hints-transact-sql-table.md). |
| 336 | +These optimizations are similar to those available with the `BULK INSERT` command. For more information, see [Table Hints (Transact-SQL)](../../t-sql/queries/hints-transact-sql-table.md). |
331 | 337 |
|
332 | 338 | ## Data Types |
333 | 339 | When you insert rows, consider the following data type behavior: |
@@ -379,7 +385,7 @@ These optimizations are similar to those available with the BULK INSERT command. |
379 | 385 | When an INSERT statement encounters an arithmetic error (overflow, divide by zero, or a domain error) occurring during expression evaluation, the [!INCLUDE[ssDE](../../includes/ssde-md.md)] handles these errors as if SET ARITHABORT is set to ON. The batch is stopped, and an error message is returned. During expression evaluation when SET ARITHABORT and SET ANSI_WARNINGS are OFF, if an INSERT, DELETE or UPDATE statement encounters an arithmetic error, overflow, divide-by-zero, or a domain error, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] inserts or updates a NULL value. If the target column is not nullable, the insert or update action fails and the user receives an error. |
380 | 386 |
|
381 | 387 | ## Interoperability |
382 | | - When an INSTEAD OF trigger is defined on INSERT actions against a table or view, the trigger executes instead of the INSERT statement. For more information about INSTEAD OF triggers, see [CREATE TRIGGER (Transact-SQL)](../../t-sql/statements/create-trigger-transact-sql.md). |
| 388 | + When an `INSTEAD OF` trigger is defined on INSERT actions against a table or view, the trigger executes instead of the INSERT statement. For more information about `INSTEAD OF` triggers, see [CREATE TRIGGER (Transact-SQL)](../../t-sql/statements/create-trigger-transact-sql.md). |
383 | 389 |
|
384 | 390 | ## Limitations and Restrictions |
385 | 391 | When you insert values into remote tables and not all values for all columns are specified, you must identify the columns to which the specified values are to be inserted. |
@@ -511,7 +517,6 @@ INSERT INTO T1 DEFAULT VALUES; |
511 | 517 | GO |
512 | 518 | SELECT column_1, column_2 |
513 | 519 | FROM dbo.T1; |
514 | | - |
515 | 520 | ``` |
516 | 521 |
|
517 | 522 | #### G. Inserting data into user-defined type columns |
|
0 commit comments