Using SQL Server stored procedures from Python (pyodbc) Following on from this thread, how do you handle spaces in variables? e.g.
for CustomerID,TestCode,TestResult in csv_input:
#query
query = """\
SET NOCOUNT ON;
Declare @CustomerID nvarchar(100),
@TestCode nvarchar(20),
@TestResult nvarchar(10),
exec TestData.sp_uat_TestData_Customer @CustomerID = ? , @TestCode = ?, @TestResult = ?;"""
#set the ID as per the iteration of the loop
args = (CustomerID,TestCode,TestResult,)
#execute the query above using the provided variable
cursor.execute(query,args)
# print out the data returned by the Stored Procedure
for row in cursor:
print(row)
csv_output.writerow(row)
In the example above @TestResult will have the value "Passed the test" in the CSV file 3rd column.
I have tried the following but with no joy: @TestResult = '''?''', @TestResult = '?', @TestResult = [?] and they all give pyodbc.ProgrammingError: ('The SQL contains 2 parameter markers, but 3 parameters were supplied', 'HY000')
I even tried adding single quotes to the value in the input CSV and still no progress. Any tips appreciated.
@TestResultdeclared as an OUTPUT parameter by chance?