I am using pandas.read_sql_query function to read from a few sql files. One query throws an error at one particular bit which I have singled out.
(python bit - nothing exotic and works with other queries)
@contextmanager
def open_db_connection(connection_string):
pyodbc.pooling = False
connection = pyodbc.connect(connection_string)
try:
yield connection
except pyodbc.DatabaseError as err:
error, = err.args
sys.stderr.write(error.message)
finally:
connection.close()
noCount = """ SET NOCOUNT ON; """
with open_db_connection(connection_string) as conn:
res = pd.read_sql_query(noCount+queryObj, conn)
The following bit of sql throws an error and I have no idea why it could be so. Preceding statements and various temp tables work and can be collected with pandas.read_sql_query(), however at the following bit it breaks.
IF OBJECT_ID('tempdb..#test1') IS NOT NULL DROP TABLE #test1;
select t.PositionID, b.SecurityID
into #test1
from #tmp as t
inner join placeholder.dbo.items as b on (b.PositionID = t.PositionID and b.StudyDate = '20191230')
where t.ast = 'eq';
IF OBJECT_ID('tempdb..#test2') IS NOT NULL DROP TABLE #test2;
select t.PositionID,
case
when count(i.beta_index)=0 then 1
else count(i.beta_index)
end as noIndex
into #test2
from #test1 as t
left join #beta_index as i on (t.SecurityID = i.isin)
group by t.PositionID;
select * from #test2
This should return data from test2. One note though - it executes and runs perfectly fine with SQL Server Management Studio.