Inserting Bulk Data in SQL Server from Text file

No.of Views1171
Bookmarked0 times
Downloads 
Votes0
By  vinothnat   On  15 Feb 2010 22:02:38
Tag : Sql Servers , T-SQL
Inserting Bulk Data in SQL Server from Text file
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This article is simple one for inserting bulk data from text file in to sql server by using a simple query.

Prerequisite

SQL Server 2005

Description

Actually in my blog, one guy asked me how to insert bulk data's in to sql server 2005.  When I tried to answer him I just wrote this article. There is a simple query to do this.

Here with I have given the query for creating a sample table and query for inserting bulk data from text file.

Query


The query for creating table,

{codecitation class="brush:sql; gutter: true;" width="500px"}

CREATE TABLE Employee

(

FName varchar (100) NOT NULL,

LName varchar (100) NOT NULL,

Email varchar (100) NOT NULL

)

{/codecitation}

Then we have a text file with the bulk datas like as follows,

vinoth,kumar,itvinoth83[at]gmail.com

emp1FName,emp1LName,emp1@company.com

emp2FName,emp2LName,emp2@company.com

emp3FName,emp3LName,emp3@company.com

emp4FName,emp4LName,emp4@company.com

emp5FName,emp5LName,emp5@company.com

emp6FName,emp6LName,emp6@company.com

emp7FName,emp7LName,emp7@company.com

emp8FName,emp8LName,emp8@company.com

emp9FName,emp9LName,emp9@company.com

emp10FName,emp10LName,emp10@company.com

emp11FName,emp11LName,emp11@company.com

emp12FName,emp12LName,emp12@company.com

Now the query for inserting bulk data is,

{codecitation class="brush:sql; gutter: true;" width="500px"}

BULK INSERT Employee FROM 'c:\bulktext.txt' WITH (FIELDTERMINATOR = ',')

{/codecitation}

Now you can see the datas inserted in table by the select query as follows,

{codecitation class="brush:sql; gutter: true;" width="500px"}

select * from Employee;

{/codecitation}

OutPut

Image loading...

Conclusion

Hence we are inserting the bulk data's in to sql server by using a simple query. The thing is that we need to keep a formatted text file with appropriate data's to insert.

Thank you

Vinoth

 
Sign Up to vote for this article
 
About Author
 
vinothnat
Occupation-Not Provided
Company-Not Provided
Member Type-Junior
Location-Not Provided
Joined date-03 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top