UPSERT method for MS SQL Server

Skip to first unread message

De Vos Meaker

Sep 22, 2021, 7:57:24 AMSep 22
to sqlalchemy

I have recently had to make a custom UPSERT method with Microsoft SQL, using their suggested MERGE method here: 

Is it within the scope of the SQLAlchemy project to create an extension for this UPSERT method through the use of MS SQL's MERGE? 

My company and I are exploring the possibility of contributing this to the project and would like to know if this is inline with the project and would get accepted?

Best regards, 
De Vos

Mike Bayer

Sep 22, 2021, 9:21:21 AMSep 22
to noreply-spamdigest via sqlalchemy
it is something that would ideally be available for the MS SQL and Oracle dialects.  We've gotten by implementing specific "upsert" constructs for SQLite, Postgresql and MySQL that are not "MERGE".  The thing is MERGE is SQL standard, although I'm not sure it's worth it to make "MERGE" a standard construct (meaning, it would be in sqlalchemy.sql) since it is so seldom used and I would guess MS SQL and Oracle have lots of quirky syntaxes in each (or maybe not).

over at we can see Oracle / MSSQL support the SQL standard implementation and MSSQL adds some extra syntaxes.

I think MERGE could be implemented for MSSQL and Oracle and it likely would need to be a sqlalchemy.sql.merge() construct since it is SQL standard.

for general guidance on how these get implemented I'd look at the commits for similar features in MySQL, Postgresql, SQLite.  For example here's sqlite:

things to look out for:

- I dont think merge() should try to also "work" on SQlite/Postgresql/Mysql , these DBs dont support the syntax and for those people need to use the dialect-specific insert() constructs
- docs for merge() have to be pretty clear this is not a generally supported construct by the most popular open source databases.  
- dialect specific documentation for MSSQL, Oracle should likely have a short section on "upsert" that indicates, "SQL Server supports SQL standard MERGE which sqlalchemy implements as sqlalchemy.sql.merge() (link to main docs)
- since MERGE is SQL standard it can have "suite tests" that are available for any potential backend, in or maybe better testing/suite/

we do have a "dream" of there being an ORM method that internally makes use of the various "upsert" or MERGE methods, which is described at .  this would solve the long-standing problem that people want to use the ORM with merge() in an automated way.  **however**, we do support an "upsert" case with the ORM right now as well that is mostly practical, which can be seen at . is also a general request for MERGE , there have been some others.  major blocker has been that people want to target multiple backends.   that issue can be reopened if you want to target on that.
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See for a full description.
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to

Reply all
Reply to author
0 new messages