I am currently executing the simply query below with python using pyodbc to insert data in SQL server table:
import pyodbc
table_name = 'my_table'
insert_values = [(1,2,3),(2,2,4),(3,4,5)]
cnxn = pyodbc.connect(...)
cursor = cnxn.cursor()
cursor.execute(
' '.join([
'insert into',
table_name,
'values',
','.join(
[str(i) for i in insert_values]
)
])
)
cursor.commit()
This should work as long as there are no duplicate keys (let's assume the first column contains the key). However for data with duplicate keys (data already existing in the table) it will raise an error. How can I, in one go, insert multiple rows in a SQL server table using pyodbc such that data with duplicate keys simply gets updated.
Note: There are solutions proposed for single rows of data, however, I would like to insert multiple rows at once (avoid loops)!
Given a dataframe(df) I used the code from ksbg to upsert into a table. Note that I looked for a match on two columns (date and stationcode) you can use one. Code generates the query given any df.
This can be done using
MERGE
. Let's say you have a key columnID
, and two columnscol_a
andcol_b
(you need to specify column names in update statements), then the statement would look like this:You can give it a try on rextester.com/IONFW62765.
Basically, I'm creating a
Source
table "on-the-fly" using the list of values, which you want to upsert. When you then merge theSource
table with theTarget
, you can test theMATCHED
condition (Target.ID=Source.ID
) on each row (whereas you would be limited to a single row when just using a simpleIF <exists> INSERT (...) ELSE UPDATE (...)
condition).In python with
pyodbc
, it should probably look like this:You can read up more on
MERGE
in the SQL Server docs.