In SQL Server 7.0, Microsoft added the capability to load data from a file into a table using the Transaction SQL extension, bulk insert statement.
This following Diagram illustrates the syntax of the bulk insert statement.
Bulk Insert Statment in SQL Server
table_name The table that you are loading data into
data_file The file containing the data
[ BATCHSIZE [= batch_size]] Specifies how many rows to load as one transaction
[[,] CHECK_CONSTRAINTS] Tells SQL Server to check column constraints when loading data
[[,] CODEPAGE [= 'ACP' | 'OEM' | 'RAW' | 'code_page']] Specifies the meaning of characters with an ascii value less than 32 or greater than 127
[[,] DATAFILETYPE [= {'char' | 'native'| 'widechar' | 'widenative'}]] Specifies that the data in the file is all character, native datatypes, unicode characters, or unicode with native datatypes
[[,] FIELDTERMINATOR [= 'field_terminator']] For character or wide character datatypes, specifies the field separator; this defaults to \t a tab
[[,] FIRSTROW [= first_row]] The first row in the file to load
[[,] FORMATFILE [= 'format_file_path']] Allows you to specify a file that describes the format of the data file
[[,] KEEPIDENTITY] Specifies to use the values in the data file for identity columns
[[,] KEEPNULLS] Specifies that empty columns gets a null assigned rather than the columns default value
[[,] KILOBYTES_PER_BATCH [= kilobytes_per_batch]] Specifies how many kilobytes will be in a batch
[[,] LASTROW [= last_row]] The last row to load
[[,] MAXERRORS [= max_errors]] Specifies to abort the bulk insert after max_errors have been reached; this option defaults to 10
[[,] ORDER ({column [ASC | DESC]} [,...n])] Specifies the order of the data int the file
[[,] ROWS_PER_BATCH [= rows_per_batch]] Specifies the number of rows per batch
[[,] ROWTERMINATOR [= 'row_terminator']] Specifies the row terminator when load char or widechar data; this defaults to\r a carriage return
[[,] TABLOCK] Tells SQL Server to lock the whole table
Notes about parameters
When using the bulk insert statement, these optional parameters affect how data is sent to SQL Server:
ROWS_PER_BATCH: The whole file is loaded as one transaction with each batch sent to SQL Server as a unit.
This parameter also controls how many rows are sent to SQL Server as a group.
BATCHSIZE: SQL Server will send each batch in its own transaction.
Note that if you use both the ROWS_PER_BATCH and the BATCHSIZE parameter the BATCHSIZE parameter will override the ROWS_PER_BATCH.
When specifying the order of the data with the ORDER option, you have to understand how SQL Server sorts the data based on its collation sequence.
If the file is not sorted correctly, or if the table does not have a clustered index in the columns specified, the ORDER option will be ignored. Collation sequence: Determines the order in which unicode data is sorted.
Specifying a format file
If you need more control over the format of the data being loaded, SQL Server allows you to specify a format file.
A format file allows you to load only specified fields from a file into a table.
A format file is useful under the following conditions:
The data file contains a different number of columns than the table or view.
The data file is in a different order than the tables or views columns.
The column delimiters vary.
Later in this module, you will learn to use the bulk copy program to generate and edit a format file. The following tooltip illustrates a format file.
Bulk Format File
To skip a field from the data file, specify prefix length of 0, server column order of 0, and no terminator.
The next lesson will cover how to use the bcp program to load and extract data from SQL Server.
Loading Data - Quiz
Click the Quiz link below to see how well you have mastered loading data with SQL Statements. Loading Data - Quiz