Welcome to our SQL Bulk Insert tutorial! This guide is designed for beginners and intermediate learners, so let's dive right in. By the end of this lesson, you'll be able to import large datasets into your SQL tables efficiently using the BULK INSERT statement.
The BULK INSERT statement is a SQL Server command used for loading large amounts of data into a table from a data file. It is more efficient than using the traditional INSERT INTO statement when dealing with huge datasets.
BULK INSERT can process large datasets more quickly than traditional INSERT INTO statements.BULK INSERT reads data directly from the file and does not require temporary storage in memory.BULK INSERT statement provides error messages and rows affected information, making it easier to troubleshoot data loading issues.BULK INSERT table_name
FROM 'path_to_file'
WITH (FORMATFILE = 'format_file_path', DATAFILE = 'data_file_path', FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');The format file is an XML file that defines the structure of the data file. You can create a format file manually or use the built-in SQL Server tool to generate one.
Let's walk through an example of using BULK INSERT to import data from a CSV file.
CREATE TABLE employees (
Id INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Age INT,
Salary DECIMAL(18, 2)
);1,John,Doe,30,50000.00
2,Jane,Smith,28,60000.00
3,Mike,Johnson,35,70000.00
Save the CSV file as employees.csv in the project directory.
Create a format file for the CSV:
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FORMATFILE="format_file_name.xml"
CODEPAGE="1252"
ROW_DELIMITOR="0x0A"
FIELD_TERMINATOR="0x2C"
/>
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" LENGTH="1"/>
<FIELD ID="2" xsi:type="CharTerm" LENGTH="50"/>
<FIELD ID="3" xsi:type="CharTerm" LENGTH="50"/>
<FIELD ID="4" xsi:type="Int4" />
<FIELD ID="5" xsi:type="Currency" />
</RECORD>
</BCPFORMAT>Save the format file as employees_format.xml in the project directory.
Execute the BULK INSERT command in SSMS:
BULK INSERT employees
FROM 'employees.csv'
WITH (FORMATFILE = 'employees_format.xml');That's it! You've successfully imported data into your SQL table using the BULK INSERT statement.
What is the main advantage of using SQL BULK INSERT over traditional INSERT INTO statements?
What is the purpose of the format file in a BULK INSERT operation?