title: SQL Server table to Excel worksheet
body: <p>I am trying to pull my SQL Server table into an Excel worksheet. When I use the following VBA code, the current status doesn't filter out to "Bound" accounts. </p>
  
  <p>But when I try not to drop the table like the second VBA code snippet below, then it works. I am using "Drop" since the execution is faster. I am not sure whether I am declaring my Drop table in VBA wrongly in the first code below. Any suggestions/ corrections will be much appreciated. </p>
  
  <pre><code> objMyCmd.CommandText = " IF OBJECT_ID('tempdb..#Step1') IS NOT NULL  DROP TABLE #Step1 " &amp; _ 
                           " SELECT a.[RDT_FileID],a.[Master Policy Number], a.[Work item /Submission no#],a.[Insured Name], a.[Credited Office], " &amp; _
                                  " a.[Credited Underwriter], a.[Product Line], a.[Product Line Subtype], a.[Current Status], a.[Effective Date], a.[Expiry Date],  a.[Premium in USD $] " &amp; _
                                  " FROM  dbo.View_Property_Rater_Of_Record a " &amp; _
                                  " WHERE a.[Master Policy Number] Is Not Null " &amp; _
                                  " AND a.[RDT_FileID] is null " &amp; _
                                  " AND a.[Product Line Subtype] &lt;&gt;  '0102-Marine' " &amp; _
                                  " AND a.[Effective Date] &gt;= '2014-04-01' " &amp; _
                                  " SELECT * from #Step1 WHERE [Current Status] ='Bound' "
  </code></pre>
  
  <p>The following is my second code for which I am not dropping table, but the process slows down drastically. Any idea on what's wrong on my first code snippet?</p>
  
  <pre><code>  objMyCmd.CommandText = " SELECT a.[RDT_FileID],a.[Master Policy Number], a.[Work item /Submission no#],a.[Insured Name], a.[Credited Office], " &amp; _
                                  " a.[Credited Underwriter], a.[Product Line], a.[Product Line Subtype], a.[Current Status], a.[Effective Date], a.[Expiry Date],  a.[Premium in USD $] " &amp; _
                                  " FROM  dbo.View_Property_Rater_Of_Record a " &amp; _
                                  " WHERE a.[Master Policy Number] Is Not Null " &amp; _
                                  " AND a.[RDT_FileID] is null " &amp; _
                                  " AND a.[Product Line Subtype] &lt;&gt;  '0102-Marine' " &amp; _
                                  " AND a.[Effective Date] &gt;= '2014-04-01' " &amp; _
                                  " AND a.[Current Status] ='Bound' "
  </code></pre>