0
votes

I have a variable like below in python;

run_id=5654

When i execute the below code;

df=curs.execute("select* from [DATABASE] where RunId=run_id")

I got an error:

DataError: ('22018', "[22018] [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting the varchar value 'run_id' to data type smallint. (245) (SQLExecDirectW)")

Could you please help me about this? How can i proceed?

1
You need to learn how to pass a variable to your query as a parameterIlyes

1 Answers

1
votes

Assuming you are connecting to your SQL Server instance using pyodbc, you should use a prepared statement with ? as the placeholder:

run_id = '5654'
df = curs.execute("SELECT * FROM [DATABASE] WHERE RunId = ?", (run_id,))