I got error message when running my code, like this:
DBAPIError: (pyodbc.Error) ('07002', '[07002] [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')
[SQL: INSERT INTO #temptable VALUES (?,?,?,?,?,?,?,?,?)]
(Background on this error at: http://sqlalche.me/e/14/dbapi)
I used to run this to import my data to SQL:
conn= urllib.parse.quote_plus("DRIVER={SQL Server}; SERVER=ABCDEF; Database=ABC; UID=user123; PWD=user123;")
engine = sa.create_engine('mssql+pyodbc:///?odbc_connect={}'.format(conn))
with engine.begin() as con:
con.execute("""
CREATE TABLE #temptable (
[a] NVARCHAR(100)
, [b] NVARCHAR(100)
, [c] NVARCHAR(100)
, [d] NVARCHAR(100)
, [e] NVARCHAR (100)
, [f] NVARCHAR (100)
, [g] NVARCHAR(100)
, [h] FLOAT
, [i] NVARCHAR(100))""")
sql_insert = f"INSERT INTO #temptable VALUES (?,?,?,?,?,?,?,?,?)"
con.execute(sql_insert, data.values.tolist())
sql_merge = """
MERGE data_final AS Target
USING #temptable AS Source
ON Source.a = Target.a
WHEN NOT MATCHED BY Target THEN
INSERT ([a], [b], [c], [d], [e], [f], [g], [h], [i])
VALUES (source.a, source.b, source.c, source.d, source.e, source.f, source.g, source.h, source.i)
WHEN MATCHED THEN
UPDATE SET
Target.h = Source.h
, Target.i = Source.i;"""
con.execute(sql_merge)
con.execute("""DROP TABLE IF EXISTS #temptable;""")
Does anyone know what's wrong in my code? Cause it was fine before I add more 2 parameter