Monday, December 8, 2008

Bulk insert into SQL server database table

The need
This comes into role when we need to insert a bulk amount of record into a table.This probably won't be coming from the user but through a text file which has defined field and record terminators.Usually if we have some pre collected data.
Implementation
The bulk insert query in sql helps us to implement this with out a parsing program.
The syntax and details can be found here
Example
We have a table called 'TestTable' in the database 'TestDB' with 2 fields 'ID' and 'Name'.
The data text file contains values which got delimeters as follows
,- separates fields
;\n -separates records
----------------
1,Joy;
2,George;
3,Hai;
4,Hello;
-----------------
Then the query is
--------------------------------
BULK INSERT TestDB.dbo.[TestTable]
FROM 'd:\test.txt'
WITH
(
FIELDTERMINATOR = ','
ROWTERMINATOR = ';\n'
)
---------------------------------

No comments: