Reputation: 137
I am trying to run a SQL command using pyodbc in python. in which SQL command contains multiple SELECT commands and IF statements.
but I am getting an error as follows
columns = [col_desc[0] for col_desc in cursor.description]
TypeError: 'NoneType' object is not iterable
import pyodbc
import pandas as pd
conn = pyodbc.connect("DRIVER={SQL Server};"
"SERVER=server_name;"
"DATABASE=master;"
"Trusted_Connection=yes;")
cursor = conn.cursor()
script="""
If object_id ('tempdb..#Temp1')is not null
drop table #Temp1
Select distinct a1.some_ID into #Temp1
from DOC.dbo.Document_tbl (NOLOCK)a1
from #Temp1 a1
If object_id ('tempdb..#Temp2')is not null
Drop table #Temp2
select distinct v2.some_data
into #Temp2 from tbl_name (nolock) v2
If object_id ('tempdb..#Results')is not null
drop table #Results
select distinct a1.*,b1.####
into #Results
from #Temp1 a1
left join #Temp2 b1 on a1.## = b1.##
Select * from #Results
"""
df = pd.read_sql(script, cnxn)
writer = pd.ExcelWriter('result.xlsx')
df.to_excel(writer, sheet_name ='bar')
writer.save()
Upvotes: 5
Views: 7308
Reputation: 126
For anyone else who is still getting this error, I have found that for some statements (A window function that aggregated null values) I also need to include SET ANSI_WARNINGS OFF;
.
Upvotes: 3
Reputation: 123849
SQL command text that contains multiple SQL statements is called an anonymous code block. An anonymous code block can return multiple results, where each result can be
The following example fails ...
sql = """\
SELECT 1 AS foo INTO #tmp;
SELECT * FROM #tmp;
"""
df = pd.read_sql_query(sql, cnxn)
# TypeError: 'NoneType' object is not iterable
... because the first SELECT ... INTO
returns a row count before the second SELECT
returns its result set.
The fix is to start the anonymous code block with SET NOCOUNT ON;
which suppresses the row count and only returns the result set:
sql = """\
SET NOCOUNT ON;
SELECT 1 AS foo INTO #tmp;
SELECT * FROM #tmp;
"""
df = pd.read_sql_query(sql, cnxn)
# no error
Upvotes: 5