BULK INSERT (T-SQL)

Copies a data file into a database table or view in a user-specified format.

Syntax
BULK INSERT [['database_name'.]['owner'].]{'table_name' FROM data_file}
[WITH
(
[ BATCHSIZE [= batch_size]]
[[,] CHECK_CONSTRAINTS]
[[,] CODEPAGE [= 'ACP' | 'OEM' | 'RAW' | 'code_page']]
[[,] DATAFILETYPE [=
{'char' | 'native'| 'widechar' | 'widenative'}]]
[[,] FIELDTERMINATOR [= 'field_terminator']]
[[,] FIRSTROW [= first_row]]
[[,] FORMATFILE [= 'format_file_path']]
[[,] KEEPIDENTITY]
[[,] KEEPNULLS]
[[,] KILOBYTES_PER_BATCH [= kilobytes_per_batch]]
[[,] LASTROW [= last_row]]
[[,] MAXERRORS [= max_errors]]
[[,] ORDER ({column [ASC | DESC]} [,...n])]
[[,] ROWS_PER_BATCH [= rows_per_batch]]
[[,] ROWTERMINATOR [= 'row_terminator']]
[[,] TABLOCK]
)
]
Arguments
'database_name'
Is the name of the database in which the specified table or view resides. If not specified, this is the current database.
'owner'
Is the name of the table or view owner. owner is optional if the user performing the bulk copy operation owns the specified table or view. If owner is not specified and the user performing the bulk copy operation does not own the specified table or view, Microsoft® SQL Server™ returns an error message, and the bulk copy operation is canceled.
'table_name'
Is the name of the table or view to bulk copy data into. Only views in which all columns refer to the same base table can be used. For more information on the restrictions for copying data into views, see INSERT.
data_file
Is the full path of the data file that contains the data to be copied into the specified table or view. BULK INSERT can copy data from a disk (including network, floppy disk, hard disk, and so on).

data_file must specify a valid path from the server on which SQL Server is running. If data_file is a remote file, specify the UNC name.

BATCHSIZE [= batch_size]
Specifies the number of rows in a batch. Each batch is copied to the server as one transaction. SQL Server commits or rolls back, in the case of failure, the transaction for every batch. By default, all data in the specified data file is one batch.
CHECK_CONSTRAINTS
Specifies that any constraints on table_name are checked during the bulk copy operation. By default, constraints are ignored.
CODEPAGE [= 'ACP' | 'OEM' | 'RAW' | 'code_page']
Specifies the code page of the data in the data file. CODEPAGE is relevant only if the data contains char, varchar, or text columns with character values greater than 127 or less than 32.

 

CODEPAGE value Description
ACP Columns of char, varchar, or text data type are converted from the ANSI/Microsoft Windows® code page (ISO 1252) to the SQL Server code page.
OEM (default) Columns of char, varchar, or text data type are converted from the system OEM code page to the SQL Server code page.
RAW No conversion from one code page to another occurs; this is the fastest option.
code_page Specific code page number, for example, 850.

DATAFILETYPE [ = {'char' | 'native' | 'widechar' | 'widenative'}]
Specifies that BULK INSERT performs the copy operation using the specified default.

 

DATAFILETYPE value Description
char (default) Performs the bulk copy operation from a data file containing character data.
native Performs the bulk copy operation using the native (database) data types of the data. The data file to be loaded was created by bulk copying data from SQL Server using the bcp utility.
widechar Performs the bulk copy operation from a data file containing Unicode characters.
widenative Performs the same bulk copy operation as native, except char, varchar, and text columns are stored as Unicode in the data file. The data file to be loaded was created by bulk copying data from SQL Server using the bcp utility. This option offers a higher performance alternative to the widechar option, and is intended for transferring data from one computer running SQL Server to another by using a data file. Use this option when you are transferring data that contains ANSI extended characters and you want to take advantage of the performance of native mode.

FIELDTERMINATOR [= 'field_terminator']
Specifies the field terminator to be used for char and widechar data files. The default is \t (tab character).
FIRSTROW [= first_row]
Specifies the number of the first row to copy. The default is 1, indicating the first row in the specified data file.
FORMATFILE [= 'format_file_path']
Specifies the full path of a format file. A format file describes the contents of the data file that contains stored responses created using the bcp utility on the same table or view. The format file should be used in cases in which:
KEEPIDENTITY
Specifies that the values for an identity column are present in the file being imported. If KEEPIDENTITY is not given, the identity values for this column in the data file being imported are ignored, and SQL Server automatically assigns unique values based on the seed and increment values specified during table creation. If the data file does not contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view should be skipped when importing data; SQL Server automatically assigns unique values for the column. For more information, see DBCC CHECKIDENT.
KEEPNULLS
Specifies that empty columns should retain a null value during the bulk copy operation, rather than have any default values for the columns inserted.
KILOBYTES_PER_BATCH [= kilobytes_per_batch]
Specifies the approximate number of kilobytes (K) of data per batch (as kilobytes_per_batch). By default, KILOBYTES_PER_BATCH is unknown.
LASTROW [= last_row]
Specifies the number of the last row to copy. The default is 0, indicating the last row in the specified data file.
MAXERRORS [= max_errors]
Specifies the maximum number of errors that can occur before the bulk copy operation is canceled. Each row that cannot be imported by the bulk copy operation is ignored and counted as one error. If max_errors is not specified, the default is 10.
ORDER ({column [ASC | DESC]} [,...n])
Specifies how the data in the data file is sorted. Bulk copy performance is improved if the data being loaded is sorted according to the clustered index on the table. If the data file is sorted in a different order, or there is no clustered index on the table, the ORDER clause is ignored. The names of the columns supplied must be valid columns in the destination table. By default, the bulk insert operation assumes the data file is unordered.
n
Is a placeholder indicating that multiple columns can be specified.
ROWS_PER_BATCH [= rows_per_batch]
Specifies the number of rows of data per batch (as rows_per_batch). Used when BATCHSIZE is not specified, resulting in the entire data file being sent to the server as a single transaction. The server optimizes the bulk load according to rows_per_batch. By default, ROWS_PER_BATCH is unknown.
ROWTERMINATOR [= 'row_terminator']
Specifies the row terminator to be used for char and widechar data files. The default is \n (newline character).
TABLOCK
Specifies that a table-level lock is acquired for the duration of the bulk copy operation. A table can be loaded concurrently by multiple clients if the table has no indexes and TABLOCK is specified. By default, locking behavior is determined by the table option table lock on bulk load. Holding a lock only for the duration of the bulk copy operation reduces lock contention on the table, thereby significantly improving performance.
Permissions

Only members of the sysadmin fixed server role can execute BULK INSERT.

Remarks

The BULK INSERT statement can be executed within a user-defined transaction. Rolling back a user-defined transaction that uses a BULK INSERT statement and BATCHSIZE clause to load data into a table or view using multiple batches rolls back all batches sent to SQL Server.

Examples

This example imports order detail information from the specified data file using a | character as the field terminator and |\n as the row terminator.

BULK INSERT Northwind.dbo.[Order Details]

    FROM 'f:\orders\lineitem.tbl'

    WITH

        (

            FIELDTERMINATOR = '|',

            ROWTERMINATOR = '|\n'

        )

  

See Also
bcp Utility Parallel Data Load Using bcp
Copying Data Using bcp or BULK INSERT sp_tableoption
Code Pages and Sort Orders Using Delimited Identifiers with bcp
Copying Data Between Different Code Pages Using the bcp Format File


(c) 1988-98 Microsoft Corporation. All Rights Reserved.