0

I have 4 parameters that I need to supply to execute Stored Procedure.

How can I simply modify this code to call stored procedure and pass multiple parameters?

import pandas as pd
import pyodbc

DateFrom  = 2
DateTo = 3
param3 = NULL
param4 = NULL

query = 'EXEC Test_procedure @DateFrom = {0}'.format(my_params)  # How to modify it here to accept multiple?

conn = pyodbc.connect('DRIVER={SQL Server};server=MySrver;DATABASE=MyDatabase;Trusted_Connection=yes;')

df = pd.read_sql_query(query, conn, params= )  #??
df
Serdia
  • 4,242
  • 22
  • 86
  • 159
  • may be [this](https://stackoverflow.com/questions/19314342/python-sqlalchemy-pass-parameters-in-connection-execute) might help. – anky Jun 13 '19 at 17:21
  • Thanks, but in my case I have a stored procedure with 1000 lines of code. Written no by me. I need to execute it and insert result into .xlsx. So the only way here would be to supply parameters – Serdia Jun 13 '19 at 17:27

1 Answers1

1

Simply extend the parameter placeholders with qmarks:

query = 'EXEC Test_procedure @DateFrom = ?, @DateTo = ?, @param3 = ?, @param4 = ?'

df = pd.read_sql(query, conn, params=[DateFrom, DateTo, None, None])

df
Parfait
  • 104,375
  • 17
  • 94
  • 125