使用 Oracle 游标插入 Sql 服务器时,大量 Pyodbc 插入无法转换为大数

Pyodbc insert of large number fails int to big to convert when inserting into Sql Server using Oracle cursor

我正在尝试执行一个 executemany 并使用 Oracle 游标插入 SQL 服务器:

sqlservercursor.executemany("INSERT INTO tablename (col1,col2...) VALUES (?,?…)",oraclecursor)

失败并出现错误:OverflowError: int too big to convert

我已将其诊断为大数字(NUMBER(25))的 id 列。

我可以使用以下方法重现:

sqlservercursor.execute('INSERT INTO tablename (Id) VALUES (?)',(90100111000002885322904,))
                

然而这有效:

sqlservercursor.execute('INSERT INTO tablename (Id) VALUES (90100111000002885322904)')

有没有办法解决这个问题,还是我必须遍历光标并手动插入?与 pyodbc 的快速执行以及必须处理字符相比,这会很慢...

我能够重现您的问题并使用 fast_executemany 解决它:

cnxn = pyodbc.connect("DSN=mssqlLocal64")
crsr = cnxn.cursor()
crsr.execute("CREATE TABLE ##tablename (Id decimal(25,0))")
data = [(90100111000002885322904,)]  # list of tuple(s)
sql = "INSERT INTO ##tablename (Id) VALUES (?)"
crsr.fast_executemany = True
crsr.executemany(sql, data)