The Wayback Machine - https://web.archive.org/web/20240618030545/https://www.geeksforgeeks.org/bulk-insert-in-sql-server/
Open In App

BULK INSERT in SQL Server(T-SQL command)

Last Updated : 12 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let’s discuss it one by one. 

Condition

Sometimes there is a scenario when we have to perform bulk insert data from .csv files into SQL Server database. We can use the GUI interface in SSMS(SQL Server Management Studio) to import data from Excel, CSV, etc files.

What if we have millions of data to be imported?

The above will be a time-consuming task so, now you will see how you can handle such kinds of operations.

SQL Server provides the BULK INSERT statement to perform large imports of data into SQL Server using T-SQL.

BULK INSERT  in SQL Server

Note – Requires INSERT and ADMINISTER BULK OPERATIONS permissions.

Approach

Here, we will provide a faster alternative to the above via few lines of T-SQL Command. 

Genera Syntax for BULK INSERT

BULK INSERT <DATABASE NAME>.<SCHEMA NAME>.<TABLE_NAME>
FROM '<FILE_PATH>'
WITH
(
-- input file format options
   [ [ , ] FORMAT = 'CSV' ]
   [ [ , ] FIELDQUOTE = 'quote_characters']
   [ [ , ] FORMATFILE = 'format_file_path' ]
   [ [ , ] FIELDTERMINATOR = 'field_terminator' ]
   [ [ , ] ROWTERMINATOR = 'row_terminator' ]
    )]
)

The most commonly used input parameters are the following as follows.

  1. FIELDTERMINATOR –‘
    ‘field_terminator’ is used as a separator between fields. The default field terminator is \t (tab character).
     
  2. ROWTERMINATOR –
    ‘row_terminator’ is used to specify the end of the row. The default row terminator is \r\n (newline character). 

Importing CSV file into SQL table

Here, we will discuss importing CSV file into a table in SQL server. Consider the given below CSV file or you can use your own csv file to perform this operation. Let us try to import a csv file into our SQL table in SQL Server.

Now, let’s create the SQL table to store the above data.

Creating table

Here we will create the table schema as per csv file records.

USE [Test]--Database name

CREATE TABLE [dbo].[Employee](                              
[Emp ID] bigint primary key,
[First Name] [varchar](50) NULL,
[Last Name] [varchar](50) NULL,
[Salary] bigint,
[City] [varchar](50)
)

Bulk Inserting

Let’s now bulk insert the file into the Employee SQL table :

BULK INSERT [Test].[dbo].[Employee]
FROM 'C:\data\employee_data.csv'
WITH (FIRSTROW = 2,
    FIELDTERMINATOR = ',',
    ROWTERMINATOR='\n' );

Output –
After executing the above operation we can see the records have been inserted with few lines of code into the Employee table like below.

References :
To get more info on the input format options, visit https://docs.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql?view=sql-server-2017#input-file-format-options.

BULK INSERT in SQL Server(T-SQL command)- FAQs

How Do I Insert Data in Bulk?

To insert data in bulk in SQL, you can use a INSERT INTO statement with multiple rows:

INSERT INTO table_name (column1, column2)

VALUES

(value1, value2),

(value3, value4),

(value5, value6);

Why Use Bulk Inserts?

Efficiency: Reduces the number of database calls, improving performance.

Speed: Faster than inserting rows one at a time, especially for large datasets.

Resource Usage: Lowers network and server resource consumption.

What is Batch Insert in SQL?

A batch insert involves grouping multiple INSERT statements into a single batch to be executed together:

BEGIN;

INSERT INTO table_name (column1, column2) VALUES (value1, value2);

INSERT INTO table_name (column1, column2) VALUES (value3, value4);

INSERT INTO table_name (column1, column2) VALUES (value5, value6);

COMMIT;

What is Bulk Load in SQL?

Bulk load is a method to quickly import large amounts of data into a database from an external source, often using specialized commands or tools.

Example Command: In SQL Server, BULK INSERT is used:

BULK INSERT table_name

FROM ‘file_path’

WITH (

FIELDTERMINATOR = ‘,’,

ROWTERMINATOR = ‘\n’

);


Next Article

Similar Reads

Bulk Reading in Cassandra
In this article, we are going to describe how we can read in bulk which also helps in improve the performance. Before going to this article please understand the basic Architecture in Cassandra. We are going to creating data schema for exercise to test the bulk reading in Cassandra. Let's have a look. First, we are going to create table. Table Sche
2 min read
Insert statement in MS SQL Server
A database contains of many tables which has data stored in an order. To add up the rows, the user needs to use insert statement. Syntax : insert into table_name(column_list) values(values_list) For better understanding, an example is given below. Example - A table named student must have values inserted into it. It has to be done as follows: inser
2 min read
Insert Into Select statement in MS SQL Server
Prerequisites - Insert statement in MS SQL Server, Select statement in MS SQL Server Consider a university database. There are two tables namely Student and Marks. In case, the marks of a few students has to be transferred from the marks table to the marks table, there are many ways to do so. One can use subqueries (query nested in another query) b
2 min read
Capturing INSERT Timestamp in Table SQL Server
While inserting data in tables, sometimes we need to capture insert timestamp in the table. There is a very simple way that we could use to capture the timestamp of the inserted rows in the table. Capture the timestamp of the inserted rows in the table with DEFAULT constraint in SQL Server. Use DEFAULT constraint while creating the table: Syntax: C
2 min read
Insert Multiple Values Into Multiple Tables in SQL Server
To insert multiple values into multiple tables in SQL Server, use the OUTPUT clause. The SQL OUTPUT clause allows to insert multiple values into multiple tables in a single query. Output clause clause in SQL is used to capture data affected by Data Manipulation Language (DML) statements like INSERT, UPDATE, or DELETE. SyntaxSyntax to insert multipl
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
Article Tags :