0

Having trouble passing parameters to a SQL Server query. The query works with raw data, but not when I try to use parameters. SQL is not identifying parameter markers.

The error I get is

The SQL contains 0 parameter markers, but 2 parameters were supplied', 'HY000'

This is my code I'm having issues with:

currenttime = datetime.datetime(year=2021, month=6, day=7, hour=7, minute=0, second=0, microsecond=999999)
print(currenttime)
print(type(currenttime))
starttime = currenttime = datetime.datetime(year=2021, month=6,day=7,hour=6,minute=0,second=0,microsecond=999999)
params = (starttime,currenttime)
sql='''
SET QUOTED_IDENTIFIER OFF
SELECT * FROM OPENQUERY(INSQL, "SELECT DateTime, [AH41_DP04]
 FROM WideHistory
 WHERE wwRetrievalMode = 'Cyclic'
 AND wwResolution = 1000
 AND wwVersion = 'Latest'
 AND DateTime >= ?
 AND DateTime <= ?")
 ''' 
print(type(params))
cursor.execute(sql,params)

for row in cursor:
    print(row)``
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • From MSDN docs: [OPENQUERY does not accept variables for its arguments.](https://learn.microsoft.com/en-us/sql/t-sql/functions/openquery-transact-sql?view=sql-server-ver15). Have Python ODBC connect to `INSQL` and run that internal `SELECT` directly with parameters. See [including parameters in OPENQUERY](https://stackoverflow.com/q/3378496/1422451). – Parfait Jun 08 '21 at 19:38
  • Ah i see. Thank you. I'm not familiar with sql. for now, i've just manipulated the string to incorporate the variables I want before sending the query. Works for now. – hmmmsomething Jun 08 '21 at 20:53

0 Answers0