I am using Python 3.9.7, pyodbc to connect to Microsoft SQL Server. I want to use string parametrize the select @MonthStartDate and select MonthEndDate variables. I tried looking at this Stack Overflow post and PyNative tutorial
query = """
Use [Data]
drop table #temp
declare @MonthStartDate date
declare @MonthEndDate datetime
declare @LastOpenDate datetime
select @MonthStartDate= ?
select @MonthEndDate= ?
select @LastOpenDate=DATEADD(dd, 7, @MonthEndDate)
select S.Keys into #temp
from DataJobs J
inner join DataSent S on J.Id=S.Id
where S.EventDate between @MonthStartDate and @MonthEndDate
group by S.Key
having count(distinct S.D)>=20
"""
params = ('2015-04-01', '2015-04-30 23:59:59.999')
cursor.execute(query, params)
var =cursor.fecthall()
I just get this error pyodbc.ProgrammingError: No results. Previous SQL was not a query.