1

I'm trying to execute a SQL stored proc within a Python script and having issues with the syntax. I have tried a combination of various examples I have found but none seem to work. Here's what I have tried:

county = 100200
confirmed = 123456
death = 12
labTestCount = 0    
#Example1
conn.execute('{CALL [spInsertCOVIDData](@County,@Confirmed,@Death,@LabTestCount)}', ('county', 'confirmed', 'death', 'labTestCount'))

#Example2
query = "EXEC [spInsertCOVIDData] (@County,@Confirmed,@Death,@LabTestCount)", (county, confirmed, death, labTestCount)
conn.execute(query)

#Example3
query = "EXEC [spInsertCOVIDData] @County=?, @Confirmed=?, @Death=?, @LabTestCount=?",(county, confirmed, death, labTestCount)
conn.execute(query)

#Example4
query = "EXEC [spInsertCOVIDData] @County='county', @Confirmed='confirmed', @Death='death', @LabTestCount='labTestCount'"
conn.execute(query)

I get a mixture of various error from:

('The SQL contains 0 parameter markers, but 4 parameters were supplied', 'HY000')

or

The first argument to execute must be a string or unicode query.
3
  • which database engine are you calling? Commented Sep 25, 2020 at 21:59
  • @E.J.Brennan SQL Server 2016 Commented Sep 25, 2020 at 22:01
  • Assuming you are using psycopg2 I am used to have the arguments in the sql string to be %s not @County, so conn.execute('{CALL [spInsertCOVIDData](%s ,%s, %s, %s)}', (val1, val2, val3, val4)) Commented Sep 26, 2020 at 2:43

1 Answer 1

1

You may try to use parameters in your statement using ? as a placeholder and pass the actual values of each parameter:

county = 100200
confirmed = 123456
death = 12
labTestCount = 0

query = "EXEC [spInsertCOVIDData] @County=?, @Confirmed=?, @Death=?, @LabTestCount=?"
conn.execute(query, (county, confirmed, death, labTestCount))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.