<?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>My Zen Arcade &#187; export</title>
	<atom:link href="http://myzenarcade.com/tag/export/feed/" rel="self" type="application/rss+xml" />
	<link>http://myzenarcade.com</link>
	<description>Here&#039;s a new idea, lets start a religion where God ISN&#039;T a bastard.</description>
	<lastBuildDate>Sun, 18 Sep 2011 11:41:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Oracle Tool: Populate Excel Sheets With Table Data</title>
		<link>http://myzenarcade.com/2009/02/24/oracle-tool-populate-excel-sheets-with-table-data/</link>
		<comments>http://myzenarcade.com/2009/02/24/oracle-tool-populate-excel-sheets-with-table-data/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 14:56:23 +0000</pubDate>
		<dc:creator>moleboy</dc:creator>
				<category><![CDATA[Science and Technology]]></category>
		<category><![CDATA[my life]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://myzenarcade.com/?p=692</guid>
		<description><![CDATA[And another tool I use a lot.
I often get asked to export all the (relatively small) tables in a schema into worksheets in an excel workbook, one per tab.
Usually, theres a back and forth first with me trying to explain how this probably won&#8217;t mean much, the user
not understanding, me explaining, the user either not [...]]]></description>
			<content:encoded><![CDATA[<!-- sphereit start --><p>And another tool I use a lot.<br />
I often get asked to export all the (relatively small) tables in a schema into worksheets in an excel workbook, one per tab.<br />
Usually, theres a back and forth first with me trying to explain how this probably won&#8217;t mean much, the user<br />
not understanding, me explaining, the user either not caring or still not understanding.<br />
Regardless, I end up having to export the data as they ask.<br />
This isn&#8217;t a big deal especially using Toad.  Unless there&#8217;s 100 tables (or, really, 15 is plenty to make it annoying).<br />
This VBA does that all for you.<br />
As before, you&#8217;ll have to build a form to bring in the relevant options (like schema, db, password).<br />
I also have it set to do its work based on a naming patterns.<br />
For example, maybe I only want tables that start with EST.  If you do or don&#8217;t want that, the relevant line is:<br />
&#8220;Select table_name from dba_tables where owner = &#8216;&#8221; &#038; inSchema &#038; &#8220;&#8216; and table_name like &#8216;&#8221; &#038; inNameString &#038; &#8220;&#8216; order by table_name&#8221;<br />
There is one limitation in that it simply can&#8217;t handle any table with a LOB column (regardless as to the data contained within)</p>
<p>Again, I promise nothing as far as HOW this is written, just the functionality.</p>
<blockquote><p>
Dim strConnect As String<br />
&#8216; Dim RS As Object<br />
    Dim conn As Object<br />
    Dim MyErrors As String<br />
    Dim MyGoodCount As Integer<br />
    Dim MyBadCount As Integer</p>
<p>Private Sub ConnectToOracle(inUID As String, inPWD As String, inServer As String)<br />
&#8216; open the connection to Oracle<br />
    strConnect = &#8220;Driver={Microsoft ODBC for Oracle};&#8221; &#038; &#8220;Server=&#8221; &#038; inServer &#038; &#8220;;uid=&#8221; &#038; inUID &#038; &#8220;;pwd=&#8221; &#038; inPWD &#038; &#8220;;&#8221;<br />
    Set conn = CreateObject(&#8221;ADODB.Connection&#8221;)<br />
    conn.ConnectionString = strConnect<br />
    conn.Open<br />
End Sub</p>
<p>Private Sub getMyRows(inSchema As String, InTable As String)<br />
    Dim RS As Object<br />
    Dim TableSQL As String<br />
    Dim DataType As String<br />
    Dim DataLength As String<br />
    Dim DataPrecision As String<br />
    Dim DataScale As String<br />
    Dim ColCount As Integer<br />
    Dim WS As Worksheet<br />
&#8216; create a sheet with the current table as name<br />
    Worksheets.Add().Name = InTable<br />
    Set RS = CreateObject(&#8221;ADODB.recordset&#8221;)<br />
On Error GoTo GetOut<br />
    TableSQL = &#8220;Select * from &#8221; &#038; inSchema &#038; &#8220;.&#8221; &#038; InTable<br />
&#8216; grab the data<br />
    RS.Open TableSQL, conn, adOpenStatic<br />
    For ColCount = 0 To RS.Fields.Count &#8211; 1<br />
&#8216; set column headings to match table<br />
       ActiveSheet.Cells(1, ColCount + 1).Value = RS.Fields(ColCount).Name<br />
    Next</p>
<p>&#8216; copy table data to sheet<br />
     With Worksheets(InTable).Range(&#8221;A2&#8243;)<br />
On Error GoTo GetOut<br />
        .CopyFromRecordset RS<br />
    End With<br />
    RS.Close</p>
<p>&#8216; a little formatting<br />
    ActiveSheet.Range(&#8221;A1&#8243;).Select<br />
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select<br />
    Selection.Columns.AutoFit<br />
    ActiveSheet.Rows(&#8221;1:1&#8243;).Select<br />
    Selection.Font.Bold = True</p>
<p>&#8216; track how many successes<br />
    MyGoodCount = MyGoodCount + 1<br />
    Exit Sub<br />
GetOut:<br />
&#8216; track errors and continue<br />
    MyErrors = MyErrors &#038; &#8220;,&#8221; &#038; InTable<br />
    MyBadCount = MyBadCount + 1<br />
End Sub<br />
Private Sub NewGetMyRows(inSchema As String, InTable As String)<br />
    Dim RST As Object<br />
    Dim TableSQL As String<br />
    Dim DataType As String<br />
    Dim DataLength As String<br />
    Dim DataPrecision As String<br />
    Dim DataScale As String<br />
    Dim ColCount As Integer<br />
    Dim WS As Worksheet</p>
<p>&#8216; this might be useful if we run into very long fields, HOWEVER, that&#8217;ll usually be clobs and those won&#8217;t work anyhow<br />
    Worksheets.Add().Name = InTable<br />
    Set RST = CreateObject(&#8221;ADODB.recordset&#8221;)<br />
    TableSQL = &#8220;Select * from &#8221; &#038; inSchema &#038; &#8220;.&#8221; &#038; InTable<br />
&#8216;    On Error GoTo GetOut<br />
    RST.Open TableSQL, conn<br />
i = 1<br />
For Each fld In RST.Fields<br />
    Worksheets(InTable).Cells(2, i).Value = fld.Name<br />
    i = i + 1<br />
Next fld</p>
<p>Dim j As Long, k As Long</p>
<p>With Worksheets(InTable)<br />
    For j = 1 To RST.RecordCount<br />
        For k = 1 To RST.Fields.Count<br />
            If IsNull(RST(k &#8211; 1)) Then<br />
                .Cells(j + 2, k) = Empty<br />
            Else<br />
                If Len(RST(k &#8211; 1)) > 255 Then<br />
                    For i = 0 To Int(Len(RST(k &#8211; 1)) / 255)<br />
                        .Cells(j + 2, k).Value = .Cells(j + 2, k).Value &#038; Mid(RST(k &#8211; 1), (i * 255) + 1, 255)<br />
                    Next i<br />
                Else<br />
                    .Cells(j + 2, k).Value = RST(k &#8211; 1)<br />
                End If<br />
            End If<br />
        Next k<br />
        RST.MoveNext<br />
    Next j<br />
End With<br />
RST.Close<br />
Exit Sub<br />
GetOut:<br />
MsgBox InTable<br />
&#8216; RST.Close<br />
End Sub</p>
<p>Sub getTables(inUser As String, inPW As String, inDB As String, inSchema As String, inNameString As String)<br />
   Dim RSTables As Object<br />
   Dim TableListSQL As String<br />
   Dim SchemaName As String<br />
   Dim errtext As String<br />
   MyGoodCount = 0<br />
   MyBadCount = 0<br />
   Set RSTables = CreateObject(&#8221;ADODB.recordset&#8221;)<br />
   ConnectToOracle inUser, inPW, inDB<br />
&#8216; grab list of tables and call getMyRows for each table<br />
   TableListSQL = &#8220;Select table_name from dba_tables where owner = &#8216;&#8221; &#038; inSchema &#038; &#8220;&#8216; and table_name like &#8216;&#8221; &#038; inNameString &#038; &#8220;&#8216; order by table_name&#8221;<br />
   RSTables.Open TableListSQL, conn, adOpenStatic<br />
   Do While Not RSTables.EOF<br />
      getMyRows inSchema, (RSTables(&#8221;table_name&#8221;))<br />
      RSTables.MoveNext<br />
   Loop<br />
   RSTables.Close<br />
&#8216; set hint if there were errors<br />
   If MyBadCount > 0 Then<br />
      errtext = &#8220;(Errors may be caused by CLOB/LOB columns)&#8221;<br />
   Else<br />
      errtext = &#8220;&#8221;<br />
   End If<br />
&#8216; report results<br />
   MsgBox &#8220;Completed!  Good:&#8221; &#038; MyGoodCount &#038; &#8221; Bad:&#8221; &#038; MyBadCount &#038; &#8221; &#8221; &#038; errtext &#038; &#8221; Bad Tables:&#8221; &#038; MyErrors</p>
<p>End Sub
</p></blockquote>
<!-- sphereit end --><span style="margin-bottom:40px; border-bottom:none;"><a class="iconsphere" title="Sphere: Related Content" onclick="return Sphere.Widget.search('http://myzenarcade.com/2009/02/24/oracle-tool-populate-excel-sheets-with-table-data/')" href="http://www.sphere.com/search?q=sphereit:http://myzenarcade.com/2009/02/24/oracle-tool-populate-excel-sheets-with-table-data/">Sphere: Related Content</a></span><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://myzenarcade.com/2009/02/24/oracle-tool-populate-excel-sheets-with-table-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

