title: Loop through SQL queries in a text file and execute - pyodbc
body: <p>I have a pipe delimited text file with SQL queries. I am trying to loop through and execute each query. When I print the queries in the file in the loop, everything seems fine. If I copy the text and run in SSMS, the SQL runs fine, but when I try to execute with pyodbc, I get the error:</p>
  
  <pre><code>ProgrammingError: No results.  Previous SQL was not a query.
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>with open('C:/users/a/tria_file_by_ASLOB12.csv', 'r') as queryFile:
      for line in queryFile:
          fields = line.split('|')
          print fields[0]
          #cursor.execute(fields[0])
          #cursor.fetchall()
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>SELECT COUNT(DISTINCT(pol_num)) In_Force_Count, sum(lmt_pol_s) Exposure
  
            FROM bapu.dbo.fact_prem 
  
            WHERE aslob = 90
  
            and CONCAT(2016,1231) between CAST(d_pol_eff AS DATE) and CAST(d_pol_exp AS DATE)
  
            and cvg_state = 'WA'
  
            and rpt_co_name in (SELECT Ent_Name FROM NAIC_Legal_Ent_Lookup WHERE ID = 40045)
  
  
  SELECT COUNT(DISTINCT(pol_num)) In_Force_Count, sum(lmt_pol_s) Exposure
  
            FROM bapu.dbo.fact_prem 
  
            WHERE aslob = 90
  
            and CONCAT(2016,1231) between CAST(d_pol_eff AS DATE) and CAST(d_pol_exp AS DATE)
  
            and cvg_state = 'WI'
  
            and rpt_co_name in (SELECT Ent_Name FROM NAIC_Legal_Ent_Lookup WHERE ID = 40045)
  </code></pre>
  
  <p>etc</p>
  
  <p>What am I missing? </p>
  
  <p>Thanks</p>