How do you use the BULK INSERT statement to import data from a file?
Posted by HenryPk
Last Updated: July 08, 2024
The BULK INSERT statement in SQL Server is used to efficiently import a large amount of data from a data file into a SQL Server database table. This operation is particularly useful for loading large datasets quickly. Below are the general steps and syntax to use the BULK INSERT statement:
Syntax
BULK INSERT [schema_name.]table_name
FROM 'file_path'
WITH (
    [OPTIONS]
);
Parameters
- schema_name.table_name: Specifies the target table's schema and name where the data will be imported. - file_path: The full path to the data file (CSV, TXT, etc.) that you want to import. - OPTIONS: A list of additional options that you can provide for the import operation. These may include: - DATAFILETYPE: Specifies the type of data. Possible values include 'native', 'char', or 'widechar'. - FIELDTERMINATOR: Character that separates fields (e.g., ',' for CSV files). - ROWTERMINATOR: Character that separates rows (e.g., '\n' for new lines). - FIRSTROW: Specifies the first row of data to import. Useful for skipping header rows. - ERRORFILE: Path to a file where error information will be logged if any errors occur during the import. - MAXERRORS: The maximum number of errors that can occur before the operation fails.
Example
Here's an example of how to use BULK INSERT to import data from a CSV file into a table named Employees:
BULK INSERT dbo.Employees
FROM 'C:\Data\employees.csv'
WITH (
    FIELDTERMINATOR = ',',    -- Comma as field separator
    ROWTERMINATOR = '\n',     -- New line as row separator
    FIRSTROW = 2,             -- Skip the header row
    ERRORFILE = 'C:\Data\BulkInsertErrors.txt',  -- Log any errors here
    MAXERRORS = 10            -- Allow up to 10 errors
);
Notes
1. Permissions: The SQL Server service account must have permissions to access the file and the folder where the file is located. 2. File Formats: The data file can be in various formats (CSV, TXT, etc.) but should match the structure of the target table. 3. Security: Consider potential security implications of using BULK INSERT and ensure that the input data is validated.
Troubleshooting
If you encounter errors while using BULK INSERT, check the following: - Ensure the file path is correct and accessible by SQL Server. - Validate that the data types in the file match the corresponding columns in the target table. - Check for any special characters or formatting that might be causing issues. Using BULK INSERT appropriately can significantly reduce the time taken to load large datasets into SQL Server.