Home > Enterprise >  COUNT field incorrect or syntax error (0) (SQLExecDirectW)')
COUNT field incorrect or syntax error (0) (SQLExecDirectW)')

Time:12-04

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

CodePudding user response:

Guessing by tags, you appear to be sending a list of lists generated from pandas data frame into execute when you should be using executemany. By the way, consider to_numpy as recommended on the DataFrame.values docs. Finally, f-string is not needed for prepared statement.

sql_insert = "INSERT INTO #temptable VALUES (?,?,?,?,?,?,?,?,?)" 
vals = data.to_numpy().tolist()

con.executemany(sql_insert, vals)

Actually, you do not even need a temp table but it may help on very large data sets to run MERGE on single set rather than iterative values.

sql_merge = """MERGE data_final AS Target 
               USING (VALUES(?,?,?,?,?,?,?,?,?)) 
                  AS Source ([a], [b], [c], [d], [e], [f], [g], [h], [i])
                    
               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;
            """
vals = data.to_numpy().tolist()

con.executemany(sql_merge, vals)
  • Related