Search code examples
pythonsqlcmd

Running SQLCmd with multiple scripting variables using Python subprocess.call


This runs fine.

C:\> SQLCmd.exe -i "myQry.sql" -v Year=2018 Month=2

But these error out.

exitcode = subprocess.call(["SQLCmd.exe", "-i", "myQry.sql", "-v", "Year=2018 Month=2"])
 Or 
exitcode = subprocess.call(["SQLCmd.exe", "-i", "myQry.sql", "-v", ['Year=2018', 'Month=2']])
ValueError: SQLCmd failed in ['-v','Year=2018 Month=2']

Looks like 'Year=2018 Month=2' is not the right way to specify multiple scripting variables in a subprocess call.

I couldn't find the answer from the documentation. Your help will be appreciated.

Thank you.


Solution

  • This should work

    exitcode = subprocess.call(["SQLCmd.exe", "-i", "myQry.sql", "-v", "Year=2018", "Month=2"])

    If you combine Year=2018 and Month=2 into one string, python will treat it as a single argument, which cannot be recognized by SQLCmd.exe