<?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>youdidwhatwithtsql.com &#187; OPENROWSET</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/openrowset/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +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>TSQL: Query Pipe-Delimited text files with OPENROWSET</title>
		<link>http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429</link>
		<comments>http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429#comments</comments>
		<pubDate>Tue, 10 Nov 2009 21:49:06 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[OPENROWSET]]></category>
		<category><![CDATA[pipe]]></category>
		<category><![CDATA[text files]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[vertical bar]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429</guid>
		<description><![CDATA[Sometimes, when working with extracts of data, it can be a pain to have to load these files into a database in order to work with them. It&#8217;s easy to use OPENROWSET to save yourself a little time. Here&#8217;s a basic example; ?View Code TSQLSELECT * FROM OPENROWSET &#40;'MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=C:\Users\Rhys\Desktop\csv;Extended [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429">TSQL: Query Pipe-Delimited text files with OPENROWSET</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when working with extracts of data, it can be a pain to have to load these files into a database in order to work with them. It&#8217;s easy to use <a href="http://technet.microsoft.com/en-us/library/ms190312.aspx" target="_blank">OPENROWSET</a> to save yourself a little time. Here&#8217;s a basic example;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p429code3'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4293"><td class="code" id="p429code3"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">OPENROWSET</span>
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'MSDASQL'</span>,
<span style="color: #FF0000;">'Driver={Microsoft Text Driver (*.txt; *.csv)}; 
DefaultDir=C:<span style="color: #000099; font-weight: bold;">\U</span>sers<span style="color: #000099; font-weight: bold;">\R</span>hys<span style="color: #000099; font-weight: bold;">\D</span>esktop<span style="color: #000099; font-weight: bold;">\c</span>sv;Extended properties='</span><span style="color: #FF0000;">'ColNameHeader=True;Format=Delimited;'</span><span style="color: #FF0000;">''</span>,
<span style="color: #FF0000;">'SELECT * FROM file1.csv'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>The <strong>DefaultDir</strong> property points to a directory containing standard comma separated files. Now the text files can be treated just like tables, note the SELECT query above in red text. There can be any number of files in the same directory and they can be queried simply by changing the appropriate filename, i.e. <strong>file1.csv</strong> to <strong>file2.txt</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_openrowset_sql_server.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv openrowset sql server" border="0" alt="csv openrowset sql server thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_openrowset_sql_server_thumb.png" width="206" height="162" /></a> </p>
<p>If you experience the below error you need to enable the feature on your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instance.</p>
<pre>Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see &quot;Surface Area Configuration&quot; in SQL Server Books Online. </pre>
<p>Run the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to enable.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p429code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4294"><td class="code" id="p429code4"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'Ad Hoc Distributed Queries'</span>, <span style="color: #000;">1</span>;
<span style="color: #0000FF;">RECONFIGURE</span>;</pre></td></tr></table></div>

</p>
<p>Now this is handy but some people, myself included, prefer to use Pipe-Delimited csv files. <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> seem insistent on calling pipes the <a href="http://en.wikipedia.org/wiki/Vertical_bar" target="_blank">vertical-bar</a>. If you attempt the above, with pipe-delimited files, here&#8217;s what you get!</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv pipe openrowset sql server" border="0" alt="csv pipe openrowset sql server thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server_thumb.png" width="188" height="163" /></a> </p>
<p>Not something we can work with very easily! By modifying a registry entry we can quickly resolve this. Usual warnings about editing your registry apply, i.e. <a href="http://support.microsoft.com/kb/322756" target="_blank">backup</a> and know what you&#8217;re doing.</p>
<p>Using <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/tools_regeditors.mspx?mfr=true" target="_blank">regedit</a>, navigate to the following key;</p>
<pre>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text</pre>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/regedit_jet_text_driver.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="regedit jet text driver" border="0" alt="regedit jet text driver thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/regedit_jet_text_driver_thumb.png" width="244" height="159" /></a> </p>
<p>Edit the value for the <strong>Format </strong>key;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jet text driver format registry" border="0" alt="text driver format registry thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry_thumb.png" width="244" height="111" /></a> </p>
<p>Change the value from <strong>CSVDelimited </strong>to <strong>Delimited(|)</strong>. To reverse this change just set the value back to <strong>CSVDelimited</strong> when you need to.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry_pipe.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jet text driver format registry pipe" border="0" alt="text driver format registry pipe thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/text_driver_format_registry_pipe_thumb.png" width="244" height="111" /></a> </p>
<p>Run the same query again and you&#8217;ll see our data is how we need it to be!</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server_fixed.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csv pipe openrowset sql server fixed" border="0" alt="csv pipe openrowset sql server fixed thumb TSQL: Query Pipe Delimited text files with OPENROWSET" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/csv_pipe_openrowset_sql_server_fixed_thumb.png" width="209" height="165" /></a> </p>
</p>
<p>Read more about the <a href="http://msdn.microsoft.com/en-us/library/bb177651.aspx" target="_blank">Text Data Source Driver</a> if you need to query files with other types of delimiters.</p>
<p><map name='google_ad_map_429_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/429?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_429_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=429&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftsql-query-pipe-delimited-text-files-with-openrowset%2F429' title="TSQL: Query Pipe Delimited text files with OPENROWSET" alt=" TSQL: Query Pipe Delimited text files with OPENROWSET" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429">TSQL: Query Pipe-Delimited text files with OPENROWSET</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/tsql-query-pipe-delimited-text-files-with-openrowset/429/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

