<?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>SQL Fool &#187; bcp</title>
	<atom:link href="http://sqlfool.com/tag/bcp/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Adventures in SQL Tuning - a blog for the rest of us</description>
	<lastBuildDate>Wed, 02 Nov 2011 20:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>BCP Basics</title>
		<link>http://sqlfool.com/2008/12/bcp-basics/</link>
		<comments>http://sqlfool.com/2008/12/bcp-basics/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 17:20:59 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[bcp]]></category>
		<category><![CDATA[large]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=222</guid>
		<description><![CDATA[In this blog post, I'm going to walk through the basics of BCP (bulk copy program). BCP is a utility that installs with SQL Server and can assist with large data transfers. Let's see what parameter options are available to use. From the command line on a machine with SQL Server installed, type "bcp" and [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog post, I'm going to walk through the basics of BCP (bulk copy program).  BCP is a utility that installs with SQL Server and can assist with large data transfers.</p>
<p>Let's see what parameter options are available to use.  From the command line on a machine with SQL Server installed, type "bcp" and press Enter.</p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_1.jpg"><img title="BCP Parameters" src="http://sqlfool.com/blogImages/20081130/BCP_1_small.jpg" alt="BCP Parameters" /></a></p>
<p>You can find out more information on BCP parameters on Books Online: <a href="http://msdn.microsoft.com/en-us/library/ms162802.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms162802.aspx</a></p>
<p>For now, we're going to examine just the basics.  The simplest syntax of a BCP command is:</p>
<p>bcp<br />
databaseName.Schema.TableName *or* "Query"<br />
in, out, *or* queryout<br />
-S ServerName\instanceName<br />
-U userName -P password *or* -T<br />
-c *or* -n *or* specify storage information for each column</p>
<p>Let's look at these options in a little more detail:</p>
<p><em>databaseName.Schema.TableName *or* Query</em><br />
You can specify either an entire table to copy or a query.  The query should be surrounded in quotations and must also include the fully qualified table name.</p>
<p><em>in, out, *or* queryout</em><br />
in = import, out = full table export, queryout = query to select data for export</p>
<p><em>-U userName -P password *or* -T</em><br />
You can either specify a specific account to access SQL Server, or use -T to indicate Trusted Connection (i.e. Windows Authentication)</p>
<p><em>-c *or* -n *or* specify storage information for each column</em><br />
-c indicates character data type, -n indicates native data type; if neither one is specified, by default you will be prompted for the data type for each column.</p>
<p>Now let's put this together and run some BCP commands.  All of these examples will use the AdventureWorks 2008 sample database.</p>
<p>First, let's export an entire table.  To do this, we'll use the "out" parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">bcp AdventureWorks.Sales.SalesOrderDetail out
C:\bcp_outputTable.txt -SYourServerName -T -c</pre></div></div>

<p>&nbsp;</p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_2.jpg"><img title="Export Table with BCP" src="http://sqlfool.com/blogImages/20081130/BCP_2_small.jpg" alt="Export Table with BCP" /></a></p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_3.jpg"><img title="Export Table with BCP - Results" src="http://sqlfool.com/blogImages/20081130/BCP_3_small.jpg" alt="Export Table with BCP - Results" /></a></p>
<p>I don't normally export an entire table... or at least, not in one process.  So let's walk through what it would look like to export the same table using a query.  This will use the "queryout" parameter.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">bcp &quot;Select SalesOrderID, SalesOrderDetailID, OrderQty, ProductID
From AdventureWorks.Sales.SalesOrderDetail&quot; queryout
C:\bcp_outputQuery.txt -SYourServerName -T -c</pre></div></div>

<p>&nbsp;</p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_4.jpg"><img title="Export Query with BCP" src="http://sqlfool.com/blogImages/20081130/BCP_4_small.jpg" alt="Export Query with BCP" /></a></p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_5.jpg"><img title="Export Query with BCP - Results" src="http://sqlfool.com/blogImages/20081130/BCP_5_small.jpg" alt="Export Query with BCP - Results" /></a></p>
<p>You'll notice that the total duration for the query was shorter than for the full-table export.  This is because we're only exporting a few of the columns.  This is important to keep in mind when bcp'ing data: you'll get better performance if you only export the data elements that you actually need.</p>
<p>Now that we've exported some data, let's walk through the process of importing this data.  First, let's create a table with a constraint that will result in some errors.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">testBCPLoad</span>
<span style="color: #808080;">&#40;</span>
      SalesOrderID          <span style="color: #0000FF;">int</span>      Not Null
    , SalesOrderDetailID    <span style="color: #0000FF;">int</span>      Not Null
    , OrderQty              <span style="color: #0000FF;">smallint</span> Null
    , ProductID             <span style="color: #0000FF;">int</span>      Null
&nbsp;
    <span style="color: #0000FF;">Constraint</span> PK_testBCPLoad
        <span style="color: #0000FF;">Primary</span> <span style="color: #0000FF;">Key</span> <span style="color: #0000FF;">Clustered</span>
        <span style="color: #808080;">&#40;</span>SalesOrderID<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;</pre></div></div>

<p>Now execute the BCP import command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">bcp sandbox.dbo.testBCPLoad in
C:\bcp_outputQuery.txt -SYourServername -T -c</pre></div></div>

<p>&nbsp;</p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_6.jpg"><img title="Load Data with BCP" src="http://sqlfool.com/blogImages/20081130/BCP_6_small.jpg" alt="Load Data with BCP" /></a></p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_7.jpg"><img title="Load Data with BCP - Error" src="http://sqlfool.com/blogImages/20081130/BCP_7_small.jpg" alt="Load Data with BCP - Error" /></a></p>
<p>You should receive a Primary Key error.  When you check your results in SQL Server, you should find no results loaded into the table.  This is BCP's default behavior.</p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_8.jpg"><img title="Check Destination Table" src="http://sqlfool.com/blogImages/20081130/BCP_8_small.jpg" alt="Check Destination Table" /></a></p>
<p>Let's change our constraint and try the same BCP command again:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Alter</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">testBCPLoad</span>
    <span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Constraint</span> PK_testBCPLoad;
&nbsp;
<span style="color: #0000FF;">Alter</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">testBCPLoad</span>
    <span style="color: #0000FF;">Add</span> <span style="color: #0000FF;">Constraint</span> PK_testBCPLoad
    <span style="color: #0000FF;">Primary</span> <span style="color: #0000FF;">Key</span> <span style="color: #0000FF;">Clustered</span>
        <span style="color: #808080;">&#40;</span>SalesOrderID, SalesOrderDetailID<span style="color: #808080;">&#41;</span>;</pre></div></div>

<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">bcp sandbox.dbo.testBCPLoad in
C:\bcp_outputQuery.txt -SYourServername -T -c</pre></div></div>

<p>You should now have the data loaded into your SQL Server destination table:</p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_9.jpg"><img title="Import Data with BCP - Results" src="http://sqlfool.com/blogImages/20081130/BCP_9_small.jpg" alt="Import Data with BCP - Results" /></a></p>
<p><a href="http://sqlfool.com/blogImages/20081130/BCP_10.jpg"><img title="Destination Table" src="http://sqlfool.com/blogImages/20081130/BCP_10_small.jpg" alt="Destination Table" /></a></p>
<p>So there you have it, the basics of BCP!  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A few BCP tips:</p>
<ul>
<li>BCP commands are case-sensitive!</li>
<li>If you're accessing the data across a WAN, perhaps via a VPN connection, try to remote desktop (mstsc) to the actual SQL Server to perform the BCP.  If possible, keep the operation on the same local drive or even local network as the server; the less distance data needs to travel across a network, the faster BCP will perform.</li>
<li>If you need to copy large amounts of data (i.e. &gt;100mm rows), try breaking the data into smaller chunks.  This will help if you have an error during BCP (i.e. a PK error can rollback the entire import operation by default, although there are options that can change this behavior).  When working with partitioned tables, I find it very efficient to segregate the data imported/exported by partition.</li>
<li>If you're BCP'ing data into a new table, you can minimize impact on the server by waiting to create your indexes after all the data is loaded.</li>
<li>I like to construct my queries in SSMS, then copy them to BCP.  Since the command-line utility does not support copy and pasting, I create a text file with my BCP command in NotePad, then save the command as a .cmd.  To execute, just call the .cmd file.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2008/12/bcp-basics/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

