<?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>Self-Professed SQL Scripting Junkie!</description>
	<lastBuildDate>Wed, 02 May 2012 21:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>BCP Script Generator</title>
		<link>http://sqlfool.com/2012/04/bcp-script-generator/</link>
		<comments>http://sqlfool.com/2012/04/bcp-script-generator/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 21:36:13 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[bcp]]></category>
		<category><![CDATA[bulk copy program]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1598</guid>
		<description><![CDATA[I&#8217;m currently working on the logic migration of data marts from SQL Server to Teradata. While another team is working on the actual table migration, it&#8217;s still helpful to have some data in the tables for developing against. The easiest method I&#8217;ve found to do this is to use BCP to export some sample data. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on the logic migration of data marts from SQL Server to Teradata. While another team is working on the actual table migration, it&#8217;s still helpful to have some data in the tables for developing against. The easiest method I&#8217;ve found to do this is to <a href="http://sqlfool.com/2008/12/bcp-basics/" target="_blank">use BCP</a> to export some sample data. So of course, I&#8217;ve created a SQL script that will generate the BCP code for me. Because that&#8217;s what I like to do on Sunday evenings.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- User-defined variables --</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @tableToBCP <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   <span style="color: #808080;">=</span> <span style="color: #FF0000;">'AdventureWorksDW2008R2.dbo.DimCustomer'</span>
    , @<span style="color: #0000FF;">Top</span>          <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>     <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- Leave NULL for all rows</span>
    , @Delimiter    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>      <span style="color: #808080;">=</span> <span style="color: #FF0000;">'|'</span>
    , @Use<span style="color: #808080;">NULL</span>      <span style="color: #0000FF;">BIT</span>             <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    , @OverrideChar <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'~'</span>
    , @MaxDop       <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'1'</span>
    , @Directory    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>    <span style="color: #808080;">=</span> <span style="color: #FF0000;">'C:\bcp_output\'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">-- Script-defined variables -- </span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @columnList <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>columnID <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @bcpStatement <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BCP &quot;SELECT '</span>
    , @currentID <span style="color: #0000FF;">INT</span>
    , @firstID <span style="color: #0000FF;">INT</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @columnList
<span style="color: #0000FF;">SELECT</span> column_id 
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
<span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> column_id;
&nbsp;
<span style="color: #0000FF;">IF</span> @<span style="color: #0000FF;">Top</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">'TOP ('</span> <span style="color: #808080;">+</span> @<span style="color: #0000FF;">Top</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">') '</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @firstID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> @columnList<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @currentID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
    <span style="color: #0000FF;">IF</span> @currentID <span style="color: #808080;">&lt;&gt;</span> @firstID
        <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">','</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> name
    <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
    <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">AND</span> column_id <span style="color: #808080;">=</span> @currentID;
&nbsp;
    <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @columnList <span style="color: #0000FF;">WHERE</span> columnID <span style="color: #808080;">=</span> @currentID;
&nbsp;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">' FROM '</span> <span style="color: #808080;">+</span> @tableToBCP 
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut '</span>
    <span style="color: #808080;">+</span> @Directory <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span>@tableToBCP, <span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.dat -S'</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">@@SERVERNAME</span>
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' -T -t&quot;'</span> <span style="color: #808080;">+</span> @Delimiter <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&quot; -c -C;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> @bcpStatement;</pre></div></div>

<p>This will generate a standard BCP script:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BCP &quot;SELECT CustomerKey,GeographyKey,CustomerAlternateKey,Title,FirstName,MiddleName,
LastName,NameStyle,BirthDate,MaritalStatus,Suffix,Gender,EmailAddress,YearlyIncome,
TotalChildren,NumberChildrenAtHome,EnglishEducation,SpanishEducation,FrenchEducation,
EnglishOccupation,SpanishOccupation,FrenchOccupation,HouseOwnerFlag,NumberCarsOwned,
AddressLine1,AddressLine2,Phone,DateFirstPurchase,CommuteDistance 
FROM AdventureWorksDW2008R2.dbo.DimCustomer WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut 
C:\bcp_output\AdventureWorksDW2008R2_dbo_DimCustomer.dat -SSQLFOOL -T -t&quot;|&quot; -c -C;</pre></div></div>

<p>However, I&#8217;ve been running into some issues with the data load. See, the thing&#8230; I&#8217;m finding that some of my tables don&#8217;t&#8230; that is to say&#8230; they may possibly contain&#8230; [whisper]<em>uncleansed data</em>[/whisper]. I know, I know&#8230; this may come as a shock to many of you, and all I ask is that you please don&#8217;t judge me for it. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>What do I mean by &#8220;uncleansed data?&#8221; I mostly mean user-inputted VARCHAR columns that contain pipes (|), tabs, carriage returns, and line feeds. These types of characters tend to mess with the data import process. Also, I&#8217;ve not yet found a way to import a data file into Teradata where a non-nullable character column contains an empty string (&#8221;). Obviously, the vast majority of the data is fine, but even one of these issues can throw an error during the import process. I&#8217;ve modified the script above to handle these specific exceptions.</p>
<p>Since I&#8217;m only using this data for testing purposes, I found it pretty easy to simply replace the offending records with ~. I&#8217;m not sure if anyone else has a need for this particular script, but I figured you could modify it pretty easily to do whatever you need.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- User-defined variables --</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @tableToBCP <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   <span style="color: #808080;">=</span> <span style="color: #FF0000;">'AdventureWorksDW2008R2.dbo.DimCustomer'</span>
    , @<span style="color: #0000FF;">Top</span>          <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>     <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- Leave NULL for all rows</span>
    , @Delimiter    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>      <span style="color: #808080;">=</span> <span style="color: #FF0000;">'|'</span>
    , @Use<span style="color: #808080;">NULL</span>      <span style="color: #0000FF;">BIT</span>             <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    , @OverrideChar <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'~'</span>
    , @MaxDop       <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'1'</span>
    , @Directory    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>    <span style="color: #808080;">=</span> <span style="color: #FF0000;">'C:\bcp_output\'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">-- Script-defined variables -- </span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @columnList <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>columnID <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @bcpStatement <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BCP &quot;SELECT '</span>
    , @currentID <span style="color: #0000FF;">INT</span>
    , @firstID <span style="color: #0000FF;">INT</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @columnList
<span style="color: #0000FF;">SELECT</span> column_id 
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
<span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> column_id;
&nbsp;
<span style="color: #0000FF;">IF</span> @<span style="color: #0000FF;">Top</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">'TOP ('</span> <span style="color: #808080;">+</span> @<span style="color: #0000FF;">Top</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">') '</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @firstID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> @columnList<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @currentID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
    <span style="color: #0000FF;">IF</span> @currentID <span style="color: #808080;">&lt;&gt;</span> @firstID
        <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">','</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> 
                            <span style="color: #0000FF;">CASE</span> 
                                <span style="color: #0000FF;">WHEN</span> user_type_id <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">231</span>, <span style="color: #000;">167</span>, <span style="color: #000;">175</span>, <span style="color: #000;">239</span><span style="color: #808080;">&#41;</span> 
                                <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'CASE WHEN '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' = '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">' THEN '</span> 
                                    <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> 
                                        <span style="color: #0000FF;">WHEN</span> is_nullable <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'NULL'</span> 
                                        <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span>@OverrideChar, max_length<span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span>
                                      <span style="color: #0000FF;">END</span>
                                    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WHEN '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span> <span style="color: #808080;">+</span> @Delimiter <span style="color: #808080;">+</span> <span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' OR '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">' + CHAR(9) + '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span> <span style="color: #008080;">-- tab</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' OR '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">' + CHAR(10) + '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span> <span style="color: #008080;">-- line feed</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' OR '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">' + CHAR(13) + '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span> <span style="color: #008080;">-- carriage return</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' THEN '</span> 
                                        <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> 
                                            <span style="color: #0000FF;">WHEN</span> is_nullable <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'NULL'</span> 
                                            <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span>@OverrideChar, max_length<span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span>
                                          <span style="color: #0000FF;">END</span>
                                    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' ELSE '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' END'</span> 
                                <span style="color: #0000FF;">ELSE</span> name 
                            <span style="color: #0000FF;">END</span> 
    <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
    <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">AND</span> column_id <span style="color: #808080;">=</span> @currentID;
&nbsp;
    <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @columnList <span style="color: #0000FF;">WHERE</span> columnID <span style="color: #808080;">=</span> @currentID;
&nbsp;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">' FROM '</span> <span style="color: #808080;">+</span> @tableToBCP 
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut '</span>
    <span style="color: #808080;">+</span> @Directory <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span>@tableToBCP, <span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.dat -S'</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">@@SERVERNAME</span>
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' -T -t&quot;'</span> <span style="color: #808080;">+</span> @Delimiter <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&quot; -c -C;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> @bcpStatement;</pre></div></div>

<p>The sample output of this would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BCP &quot;SELECT CustomerKey,GeographyKey,CASE WHEN CustomerAlternateKey = '' THEN 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' WHEN CustomerAlternateKey LIKE '%|%' OR 
CustomerAlternateKey LIKE '%' + CHAR(9) + '%' OR CustomerAlternateKey LIKE 
'%' + CHAR(10) + '%' OR CustomerAlternateKey LIKE '%' + CHAR(13) + '%' 
THEN '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' ELSE CustomerAlternateKey END,
CASE WHEN Title = '' THEN NULL WHEN Title LIKE '%|%' OR Title LIKE '%' + CHAR(9)
 + '%' OR Title LIKE '%' + CHAR(10) + '%' OR Title LIKE '%' + CHAR(13) + '%' 
THEN NULL ELSE Title END,CASE WHEN FirstName = '' THEN NULL WHEN FirstName 
LIKE '%|%' OR FirstName LIKE '%' + CHAR(9) + '%' OR FirstName LIKE '%' + 
CHAR(10) + '%' OR FirstName LIKE '%' + CHAR(13) + '%' THEN NULL ELSE 
FirstName END,CASE WHEN MiddleName = '' THEN NULL WHEN MiddleName LIKE '%|%'
 OR MiddleName LIKE '%' + CHAR(9) + '%' OR MiddleName LIKE '%' + CHAR(10) +
 '%' OR MiddleName LIKE '%' + CHAR(13) + '%' THEN NULL ELSE MiddleName END,
CASE WHEN LastName = '' THEN NULL WHEN LastName LIKE '%|%' OR LastName LIKE
 '%' + CHAR(9) + '%' OR LastName LIKE '%' + CHAR(10) + '%' OR LastName LIKE
 '%' + CHAR(13) + '%' THEN NULL ELSE LastName END,NameStyle,BirthDate,CASE 
WHEN MaritalStatus = '' THEN NULL WHEN MaritalStatus LIKE '%|%' OR 
MaritalStatus LIKE '%' + CHAR(9) + '%' OR MaritalStatus LIKE '%' + CHAR(10) 
+ '%' OR MaritalStatus LIKE '%' + CHAR(13) + '%' THEN NULL ELSE MaritalStatus 
END,CASE WHEN Suffix = '' THEN NULL WHEN Suffix LIKE '%|%' OR Suffix LIKE '%' 
+ CHAR(9) + '%' OR Suffix LIKE '%' + CHAR(10) + '%' OR Suffix LIKE '%' + 
CHAR(13) + '%' THEN NULL ELSE Suffix END,CASE WHEN Gender = '' THEN NULL 
WHEN Gender LIKE '%|%' OR Gender LIKE '%' + CHAR(9) + '%' OR Gender LIKE '%' 
+ CHAR(10) + '%' OR Gender LIKE '%' + CHAR(13) + '%' THEN NULL ELSE Gender 
END,CASE WHEN EmailAddress = '' THEN NULL WHEN EmailAddress LIKE '%|%' OR 
EmailAddress LIKE '%' + CHAR(9) + '%' OR EmailAddress LIKE '%' + CHAR(10) + 
'%' OR EmailAddress LIKE '%' + CHAR(13) + '%' THEN NULL ELSE EmailAddress END,
YearlyIncome,TotalChildren,NumberChildrenAtHome, CASE WHEN EnglishEducation = '' 
THEN NULL WHEN EnglishEducation LIKE '%|%' OR 
EnglishEducation LIKE '%' + CHAR(9) + '%' OR EnglishEducation LIKE '%' + 
CHAR(10) + '%' OR EnglishEducation LIKE '%' 
+ CHAR(13) + '%' THEN NULL ELSE EnglishEducation END,CASE WHEN 
SpanishEducation = '' THEN NULL WHEN SpanishEducation LIKE '%|%' OR 
SpanishEducation LIKE '%' + CHAR(9) + '%' OR SpanishEducation LIKE '%' + 
CHAR(10) + '%' OR SpanishEducation LIKE '%' + CHAR(13) + '%' THEN NULL 
ELSE SpanishEducation END,CASE WHEN FrenchEducation = '' THEN NULL WHEN 
FrenchEducation LIKE '%|%' OR FrenchEducation LIKE '%' + CHAR(9) + '%' 
OR FrenchEducation LIKE '%' + CHAR(10) + '%' OR FrenchEducation LIKE '%' 
+ CHAR(13) + '%' THEN NULL ELSE FrenchEducation END,CASE WHEN 
EnglishOccupation = '' THEN NULL WHEN EnglishOccupation LIKE '%|%' OR 
EnglishOccupation LIKE '%' + CHAR(9) + '%' OR EnglishOccupation LIKE '%' 
+ CHAR(10) + '%' OR EnglishOccupation LIKE '%' + CHAR(13) + '%' THEN 
NULL ELSE EnglishOccupation END,CASE WHEN SpanishOccupation = '' THEN 
NULL WHEN SpanishOccupation LIKE '%|%' OR SpanishOccupation LIKE '%' 
+ CHAR(9) + '%' OR SpanishOccupation LIKE '%' + CHAR(10) + '%' OR 
SpanishOccupation LIKE '%' + CHAR(13) + '%' THEN NULL ELSE SpanishOccupation 
END,CASE WHEN FrenchOccupation = '' THEN NULL WHEN FrenchOccupation LIKE 
'%|%' OR FrenchOccupation LIKE '%' + CHAR(9) + '%' OR FrenchOccupation 
LIKE '%' + CHAR(10) + '%' OR FrenchOccupation LIKE '%' + CHAR(13) + '%' 
THEN NULL ELSE FrenchOccupation END,CASE WHEN HouseOwnerFlag = '' THEN 
NULL WHEN HouseOwnerFlag LIKE '%|%' OR HouseOwnerFlag LIKE '%' + CHAR(9) 
+ '%' OR HouseOwnerFlag LIKE '%' + CHAR(10) + '%' OR HouseOwnerFlag LIKE 
'%' + CHAR(13) + '%' THEN NULL ELSE HouseOwnerFlag END,NumberCarsOwned,CASE 
WHEN AddressLine1 = '' THEN NULL WHEN AddressLine1 LIKE '%|%' OR AddressLine1 
LIKE '%' + CHAR(9) + '%' OR AddressLine1 LIKE '%' + CHAR(10) + '%' OR 
AddressLine1 LIKE '%' + CHAR(13) + '%' THEN NULL ELSE AddressLine1 END,CASE 
WHEN AddressLine2 = '' THEN NULL WHEN AddressLine2 LIKE '%|%' OR AddressLine2 
LIKE '%' + CHAR(9) + '%' OR AddressLine2 LIKE '%' + CHAR(10) + '%' OR 
AddressLine2 LIKE '%' + CHAR(13) + '%' THEN NULL ELSE AddressLine2 END,CASE 
WHEN Phone = '' THEN NULL WHEN Phone LIKE '%|%' OR Phone LIKE '%' + CHAR(9) 
+ '%' OR Phone LIKE '%' + CHAR(10) + '%' OR Phone LIKE '%' + CHAR(13) + '%' 
THEN NULL ELSE Phone END,DateFirstPurchase,CASE WHEN CommuteDistance = '' 
THEN NULL WHEN CommuteDistance LIKE '%|%' OR CommuteDistance LIKE '%' + 
CHAR(9) + '%' OR CommuteDistance LIKE '%' + CHAR(10) + '%' OR CommuteDistance 
LIKE '%' + CHAR(13) + '%' THEN NULL ELSE CommuteDistance END 
FROM AdventureWorksDW2008R2.dbo.DimCustomer WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut 
C:\bcp_output\AdventureWorksDW2008R2_dbo_DimCustomer.dat -SSQLFOOL -T -t&quot;|&quot; -c -C;</pre></div></div>

<p>I don&#8217;t know about you, but that would take me a bit longer to write manually than to execute the script above. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One note: I&#8217;ve found that copying this code into a batch file will actually render the CHAR functions, i.e. CHAR(13) will be replaced with a carriage return in the script. To avoid this, copy and paste the BCP script directly into your command window. </p>
<p>Not familiar with BCP? Refer to my blog post on <a href="http://sqlfool.com/2008/12/bcp-basics/" target="_blank">BCP Basics</a> to help get you started.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/04/bcp-script-generator/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<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&#8217;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&#8217;s see what parameter options are available to use. From the command line on a machine with SQL Server installed, type &#8220;bcp&#8221; and [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog post, I&#8217;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&#8217;s see what parameter options are available to use.  From the command line on a machine with SQL Server installed, type &#8220;bcp&#8221; 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&#8217;re going to examine just the basics.  The simplest syntax of a BCP command is:</p>
<p>bcp<br />
databaseName.Schema.TableName *or* &#8220;Query&#8221;<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&#8217;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&#8217;s put this together and run some BCP commands.  All of these examples will use the AdventureWorks 2008 sample database.</p>
<p>First, let&#8217;s export an entire table.  To do this, we&#8217;ll use the &#8220;out&#8221; 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&#8217;t normally export an entire table&#8230; or at least, not in one process.  So let&#8217;s walk through what it would look like to export the same table using a query.  This will use the &#8220;queryout&#8221; 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&#8217;ll notice that the total duration for the query was shorter than for the full-table export.  This is because we&#8217;re only exporting a few of the columns.  This is important to keep in mind when bcp&#8217;ing data: you&#8217;ll get better performance if you only export the data elements that you actually need.</p>
<p>Now that we&#8217;ve exported some data, let&#8217;s walk through the process of importing this data.  First, let&#8217;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&#8217;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&#8217;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&#8217;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&#8217;re BCP&#8217;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>25</slash:comments>
		</item>
	</channel>
</rss>

