'pyobdc Update query executes successfully but doesnt update database

I have a problem with executing an MS SQL Update query with pyodbc. The query executes successfully but the database entries are not being updated. Running the same query in sql management studio return an successfull update of over 800 entries. What am I doing wrong?

import pyodbc
    
conn = pyodbc.connect('Driver={SQL Server};'
                          'Server=SERVER2020;'
                          'Database=Auftragsmanagement;'
                          'Trusted_Connection=yes;')
    
sql_einkaufspreis_in_artikel_schreiben = "UPDATE dbo.Artikel " \
                                            "SET Artikel.Einkaufspreis = t.Einkaufspreis " \
                                            "from dbo.Artikel A " \
                                            "Inner Join " \
                                            "(select a.id, " \
                                                "(sum(IIF(e.kategorie NOT IN (7, 10), r.menge*IIF(a.NutzeManuellenPreisInKalkulation = 1 AND r.manuellerPreis != 0, r.manuellerPreis, IIF(t.preis IS NOT NULL,  t.preis/e.Berechnungsmenge, e.preis/e.Berechnungsmenge))/(1-a.Kalkulation_Verlust/100), 0))/" \
                                                "sum(IIF(e.kategorie NOT IN (7, 10, 3, 26), r.menge, 0))+" \
                                                "sum(IIF(e.kategorie IN (7, 10), r.menge*IIF(a.NutzeManuellenPreisInKalkulation = 1 AND r.manuellerPreis != 0, r.manuellerPreis, IIF(t.preis IS NOT NULL, t.preis/e.Berechnungsmenge, e.preis/e.Berechnungsmenge)), 0)))*a.Kalkulationsgewicht AS Einkaufspreis " \
                                            "from dbo.Artikel A " \
                                            "INNER JOIN dbo.Rezeptur R ON R.Artikel = a.id " \
                                            "INNER JOIN dbo.Einkaufsartikel E ON r.einkaufsartikel = e.id " \
                                            "INNER JOIN dbo.Lieferant L ON l.id = e.lieferant " \
                                            "LEFT JOIN dbo.EinkaufsartikelMapping EPM ON epm.Einkaufsartikel = e.ID " \
                                            "LEFT JOIN dbo.EinkaufsartikelSpezifikation EP ON ep.id = epm.EinkaufsartikelSpezifikation " \
                                            "FULL OUTER JOIN (SELECT ep.id, e.Preis, e.LetzteLieferung " \
                                                            "FROM (" \
                                                                "SELECT ROW_NUMBER() OVER (PARTITION BY EP.ID ORDER BY e.LetzteLieferung DESC) AS LASTORDER, e.id " \
                                                                "FROM dbo.Einkaufsartikel E, dbo.EinkaufsartikelSpezifikation EP, dbo.EinkaufsartikelMapping EPM " \
                                                                "WHERE e.id = epm.Einkaufsartikel and ep.id = epm.EinkaufsartikelSpezifikation) as list, dbo.Einkaufsartikel E " \
                                                            "INNER JOIN dbo.EinkaufsartikelMapping EPM ON e.id = epm.Einkaufsartikel " \
                                                            "INNER JOIN dbo.EinkaufsartikelSpezifikation EP ON epm.EinkaufsartikelSpezifikation = ep.ID " \
                                                            "where list.ID = e.ID and list.LASTORDER <= 1 " \
                                                            "GROUP BY ep.id, e.LetzteLieferung, e.Preis) t ON t.ID = ep.ID " \
                                            "where a.kategorie != 99 and a.IstKalkuliert = 1 and a.Gelöscht = 0 " \
                                            "group by a.id, a.Kalkulationsgewicht) t ON t.id = a.ID"
    
cursor = conn.cursor()
cursor.execute(sql_einkaufspreis_in_artikel_schreiben)
cursor.commit()
cursor.close()
conn.close()

Are there any limitations in pyodbc when trying to update rows?

cursor.rowcount returns 865 rows. Why does is return an value but doesn't update.

EDIT

Adding SET NOCOUNT ON at the beginning of the query solves the problem, but then I won't have the number of affected rows. Is there another solution for that?



Solution 1:[1]

Try it with a very simple example, get that working, and then you can move on to more complex examples. What you have now is pretty complex.

See the links below for more details.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html

https://datatofish.com/import-csv-sql-server-python/

https://www.dataquest.io/blog/sql-insert-tutorial/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ASH