Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 352, comments - 144, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

SQL Server 2005 Bulk Inserts

Need to dump a large amount of data into sql server? Have I got some tools for you! Recently I needed to, from a stored procedure, copy a large amount of data from a text file on the disk into my sql server. This is nice an easy with SQL Server 2005. You can write a simple INSERT INTO statement that will copy the data out of the flat file into your table. Instead of providing a table name in the from part of your insert just use the OPENROWSET option with the BULK keyword and poof your data is in the database. The only trick is to creating the XML format file that describes to SQL Server what the text file looks like. But that is much simpler if you use the bcp utility with the format option to generate it (look about halfway down the page on the third link below). Happy importing!

Here is an example of the SQL: (easily embeddable in any stored procedure)
INSERT INTO destination_table
SELECT
 column1,
 column2,
 column3,
 column4
  FROM  OPENROWSET(BULK '\\server\folder\source.txt',
 FORMATFILE='\\server\folder\format.xml'    
  ) as t1 ;

Here is an example of the XML format file:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <RECORD>
  <FIELD ID="1" xsi:type="CharFixed" LENGTH="8" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
  <FIELD ID="2" xsi:type="CharFixed" LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
  <FIELD ID="3" xsi:type="CharFixed" LENGTH="32" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
  <FIELD ID="4" xsi:type="CharTerm" MAX_LENGTH="60" TERMINATOR="\r\n" COLLATION="SQL_Latin1_General_CP1_CI_AS" />
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="column1" xsi:type="SQLINT" />
  <COLUMN SOURCE="2" NAME="column2" xsi:type="SQLVARYCHAR" LENGTH = "30" />
  <COLUMN SOURCE="3" NAME="column3" xsi:type="SQLVARYCHAR" LENGTH = "32" />
  <COLUMN SOURCE="4" NAME="column4" xsi:type="SQLVARYCHAR" LENGTH = "60" />
 </ROW>
</BCPFORMAT>


Useful links:

OPENROWSET (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms190312.aspx

Importing Bulk Data by Using BULK INSERT or OPENROWSET(BULK...)
http://msdn2.microsoft.com/en-us/library/ms175915.aspx

Using a Format File to Bulk Import Data
http://msdn2.microsoft.com/en-us/library/ms178129.aspx

Print | posted on Friday, October 26, 2007 7:14 PM | Filed Under [ Microsoft SQL ]

Feedback

Gravatar

# re: SQL Server 2005 Bulk Inserts

Hello all,

Please i need to post some of my data's about 20,000 to my remote database server, I tried using SQL INSERT Statement and a do looping with WHILE but it's taking forever.

Any body with a good solution ? Help me.

Thanks all
11/21/2009 7:59 AM | abraham

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 6 and type the answer here:

Powered by: