Bulk Insert Command In SQL Server 2008

In this article I describe about bulk insert command in SQL Server.
  • 2024

In this article I describe about bulk insert command in SQL Server. Bulk Insert command can be executed within stored procedure or T-SQL script to import data in to SQL Server.

Create Table

CREATE TABLE  Student

(

Emp_FirstName varchar (100) NOT NULL,

Emp_LastName varchar (100) NOT NULL,

Email varchar (100) NOT NULL

)

Student Table

select * from Student

EmptyTable.jpg

Till now data is not inserted in table.

Bulk Insert Command

Suppose we have a text file named StudentDetails.Text  which contains large amount of data. Now we want to insert it in our database. Column Name must be same that you have data in text file.  Bulk Insert command inserts data from text file into destination table by using one-to-one mapping.

If destination table has too many columns then this method may no work, it will fail. The file location of the source comma-delimited file, and the options are passed as a parameter of the command specify the table receiving the data.

Example

Suppose we have StudentDetails.Txt file.

TextFile.jpg

To insert data from StudentDetails.Txt file in Student table, write following query.

BULK INSERT Student FROM 'd:\bulkInsert.txt' WITH (FIELDTERMINATOR = ',')

Now select Student table records.

select * from Student

BulkInsert_sql.jpg

Note: While using Bulk Insert command, it will more helpful to open the source file using Excel file.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.