У меня есть параметр, который я передал в инструкцию SELECT, но он обрезается до первого символа.
Вход CSV выглядит следующим образом (2 параметра):
87213T12,Code1
Код следующим образом:
##Declare the list of data to pass into the Query
def check_sql_string(sql, values):
unique = "%PARAMETER%"
sql = sql.replace("?", unique)
for v in values: sql = sql.replace(unique, repr(v), 1)
return sql
##Declare the list of data to pass into the Query
dirname = os.path.dirname(os.path.abspath(__file__))
csvfilenameinput = os.path.join(dirname, 'test2.csv')
csvfilenameoutput = os.path.join(dirname, 'Log2.csv')
with open(csvfilenameinput, newline='') as DN_input, open(csvfilenameoutput, 'w', newline='') as DN_output:
csv_input = csv.reader(DN_input)
csv_output = csv.writer(DN_output)
##skip header line
next(csv_input)
##loop through the records above
print("Data Loaded")
for customerid,TestCode in csv_input:
#query similar to that developed by Data Team
query = """\
DECLARE @return_value int;
Declare @customerid nvarchar(100),
@TestCode nvarchar(20)
set @customerid = ?
set @TestCode = ?
exec @return_value =UAT_TestData.sp_uat_TestData @customerid , @TestCode
SELECT @return_value;"""
#set the ID as per the iteration of the loop
args = (customerid,TestCode)
#execute the query above using the provided variable
cursor.execute(query,(args))
print(check_sql_string(query, (args)))
print("******SP executed")
conn.commit()
print("******Commit")
#confirm the result is as expected by executing the SELECT query and placing in the LOG file
## *******Issue here***********
selectquery="""
Select * from table.tabletest where custid = ? """
cursor.execute(selectquery, (customerid))
print(check_sql_string(selectquery, (customerid)))
print("*****query executed")
selectResults = cursor.fetchall()
print("*****Results in FETCHALL")
# print out the data created by the Select
SelectDataReturned=len(selectResults)
if SelectDataReturned>0:
for row in selectResults:
print("****For loop entered")
print(row)
csv_output.writerow(row)
print("***For loop ended")
кроме: print («Исключение произошло выше!»)
Любые указатели на то, почему «customerid» обрезается в SELECT до 1 символа, но не в EXECUTE Хранимая процедура? вывод SELECT оператора SQL выглядит следующим образом:
select * from table.tabletest where custid = '8'
первый символ отображается только тогда, когда он должен быть 87213T12