<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Serge&#039;s Technology View &#187; import csv</title>
	<atom:link href="http://blog.dragonsoft.us/tag/import-csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dragonsoft.us</link>
	<description>Talk about Technologies, Software Architecture and Management</description>
	<lastBuildDate>Tue, 31 Jan 2012 01:43:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Load text file data into SQL Server table</title>
		<link>http://blog.dragonsoft.us/2008/03/06/load-text-file-data-into-sql-server-table/</link>
		<comments>http://blog.dragonsoft.us/2008/03/06/load-text-file-data-into-sql-server-table/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 17:36:40 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[import csv]]></category>
		<category><![CDATA[import text file]]></category>
		<category><![CDATA[sql server]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/2008/03/06/load-text-file-data-into-sql-server-table/</guid>
		<description><![CDATA[Last time we have seen how to manipulate text files from SQL Server code using Scripting object. This time we would look at &#8220;Read File As Table&#8221; operation from a different angle &#8211; using BULK INSERT operation. There are pros and &#8230; <a href="http://blog.dragonsoft.us/2008/03/06/load-text-file-data-into-sql-server-table/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last time we have seen how to <a href="http://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/">manipulate text files from SQL Server</a> code using Scripting object.</p>
<p>This time we would look at &#8220;<strong>Read File As Table</strong>&#8221; operation from a different angle &#8211; using BULK INSERT operation.</p>
<p>There are pros and cons in this approach. Lets look at them.</p>
<p>Advantages:</p>
<ul>
<li>Getting data in table format, with properly associated columns</li>
<li>BULK INSERT is quicker operation if you know what you are importing</li>
<li>IDENTITY INSERT and other Batch optimizers</li>
<li>Transaction support</li>
<li>Is easiest way to copy data between servers</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li>it is supported for variation of CSV formatted files only</li>
<li>structure of the file is strict and small error in formatting or a hidden character can cause problems with import</li>
<li>only members of the <strong>sysadmin</strong> and <strong>bulkadmin</strong> server roles can perform an operation</li>
<li>could be messy if not properly managed.</li>
</ul>
<p>All saying, you should always try and see if it works for you, instead of creating custom import procedures or using DTS functionality.</p>
<p>Let fun begins:</p>
<p>We would be working with the following table:</p>
<pre class="brush: sql; title: ; notranslate">CREATE TABLE StoreList (
StoreID INT,
StoreName VARCHAR(32),
  City VARCHAR(32),
  State VARCHAR(2),
  Country VARCHAR(32)
)</pre>
<p>Sample data we would be using is stored in CSV file called c:\stores.csv</p>
<pre>1111,Stone Age Books,Boston,MA,USA
2222,Harley &amp; Davidson,Washington,DC,USA
3333,Infodata Algosystems,Berkeley,CA,USA</pre>
<p>When table is created Bulk insert could be performed using the following command:</p>
<pre class="brush: sql; title: ; notranslate">BULK INSERT dbo.StoreList FROM 'c:\stores.csv' WITH (
   FIELDTERMINATOR = ',',
   ROWTERMINATOR = '\n'
)
</pre>
<p>Notice that we are using coma as separator, it might not be appropriate in some situations and could be easily addressed for let say TAB-separated values using</p>
<pre class="brush: sql; title: ; notranslate">BULK INSERT dbo.StoreList FROM 'c:\stores.csv'
WITH (
   ROWTERMINATOR = '\n'
)</pre>
<p>As you can see that FIELDTERMINATOR has been removed. This is a case because &#8216;\t&#8217; value otherwise used here is a default for the parameter. You can adjust Field and Row terminators to better suite format present in the file in each case.</p>
<p>Another thing to remember that some of the files, especially results from Excel export might include header rows which might make your import fail with conversion error</p>
<blockquote><p>Bulk insert data conversion error (type mismatch) for&#8230;</p></blockquote>
<p>To address the issue simply exclude header related rows from import</p>
<pre class="brush: sql; title: ; notranslate">BULK INSERT dbo.StoreList FROM 'c:\stores.csv'
WITH (
    FIRSTROW = 2,
    ROWTERMINATOR = '\n'
)
</pre>
<p>There are other settings which might affect bulk import:</p>
<ul>
<li>Turning Insert Triggers On while performing import</li>
<li>Keep NULL values &#8211; situation when there is nothing specified between delimiters</li>
<li>specifying LASTROW of the file to import</li>
<li>how many error to ignore when found in the file before invalidating a whole import</li>
</ul>
<p>Please refer to SQL Server Book Online for details about such settings.</p>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2008/03/06/load-text-file-data-into-sql-server-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

