SERPland Blog

PL/SQL: Merge Statement - Merge into a single table (no Join to insert or update) - like mySQL upsert

· 934 words · 5 minutes to read

On Oracle, you can write simple insert/update statements with PL/SQL. Also using some if-then-else cases you can decide wether to insert or to update data.

The much more elegant way to insert or update data with pl/sql is to use Oracle’s MERGE Command (like upsert in mySQL). 🔗

Usually within the Merge command, you can join two tables together to check the data. Just search the web - you’ll find many examples with MERGE and JOINS.

But what when you’d only like to MERGE into a single table?

After some tries, I found a solution using the DUAL table. Just see my MERGE PL/SQL sample. It’s absolutelly easy to understand:

merge into position_table t using dual on (t.pos_id = p_pos_id) when matched then update set anzahl = p_anzahl , gueltig_ab = p_gueltig_ab , doc_id = p_doc_id , bemerkung = p_bemerkung when not matched then insert (pos_id ,anzahl ,gueltig_ab ,doc_id ,bemerkung ) values (p_pos_id ,p_anzahl ,p_gueltig_ab ,p_doc_id ,p_bemerkung ) ; – ende merge


Update 2024

Update zum PL SQL Merge Statement 🔗

Im Jahr 2011 wurde im Artikel über das PL SQL Merge Statement diskutiert, wie man Daten in Oracle in eine einzelne Tabelle zusammenführen kann, ohne einen Join zu verwenden, um Daten einzufügen oder zu aktualisieren, ähnlich wie bei mySQL upsert.

Im Jahr 2024 hat sich die Verwendung von PL SQL in Oracle-Datenbanken weiterentwickelt, und das Merge-Kommando wird immer noch als elegante Lösung für das Zusammenführen von Daten angesehen.

Die Möglichkeit, einfache Insert-Update-Anweisungen mit PL SQL zu schreiben, besteht immer noch, aber das Merge-Kommando bietet eine effizientere Möglichkeit, dies zu erreichen. Durch die Verwendung des Merge-Kommandos können Daten entweder aktualisiert oder eingefügt werden, je nach Bedingung, ohne dass separate Anweisungen geschrieben werden müssen.

In Bezug auf das Problem, Daten ohne Verwendung eines Joins in eine einzelne Tabelle zusammenzuführen, bietet Oracle nach wie vor keine direkte Möglichkeit, dies mit dem Merge-Kommando zu erreichen. Eine mögliche Lösung besteht darin, die DUAL-Tabelle zu verwenden, wie im vorherigen Artikel erwähnt.

Hier ist ein aktualisiertes Beispiel für das Merge PL SQL Statement, das in Oracle im Jahr 2024 funktionieren würde:

merge into position_table t
using dual
on (t.pos_id = p.pos_id)
when matched then
  update set
    t.anzahl = p.anzahl,
    t.gueltig_ab = p.gueltig_ab,
    t.doc_id = p.doc_id,
    t.bemerkung = p.bemerkung
when not matched then
  insert (pos_id, anzahl, gueltig_ab, doc_id, bemerkung)
  values (p.pos_id, p.anzahl, p.gueltig_ab, p.doc_id, p.bemerkung);

Es ist wichtig zu beachten, dass sich die Syntax möglicherweise im Laufe der Jahre geringfügig geändert hat, aber das grundlegende Konzept des Merge-Kommandos in PL SQL in Oracle-Datenbanken bleibt bestehen.

Wenn Sie nach weiteren Beispielen und Anleitungen zum Merge-Kommando und Joins in Oracle suchen, stehen Ihnen weiterhin zahlreiche Ressourcen im Internet zur Verfügung, die Ihnen bei der Implementierung und Optimierung Ihrer Datenbankabfragen helfen können.

Insgesamt hat sich die Verwendung des PL SQL Merge Statements in den letzten Jahren als zuverlässige Methode zum Aktualisieren und Einfügen von Daten in Oracle-Datenbanken bewährt, und Entwickler können weiterhin von seiner Leistungsfähigkeit und Eleganz profitieren.


2025 Anleitungs-Beschreibung (Instruction Manual)

PLSQL Merge Statement: Merging Data into a Single Table without Join 🔗

Are you looking for a way to efficiently insert or update data in Oracle without the need for complex join statements? The PLSQL Merge Statement provides a simple and elegant solution for merging data into a single table, similar to the upsert functionality in mySQL.

Using the Merge Command in PLSQL 🔗

In Oracle, you can write insert-update statements with PLSQL, but using the Merge Command offers a more efficient approach. Typically, the Merge Command involves joining two tables together to check the data. However, if you only want to merge data into a single table, a workaround using the DUAL table can be implemented.

Here is a sample PLSQL Merge Statement:

merge into positiontable t
using dual
on t.posid = p.posid
when matched then update
set anzahl = p.anzahl,
    gueltigab = p.gueltigab,
    docid = p.docid,
    bemerkung = p.bemerkung
when not matched then insert
(posid, anzahl, gueltigab, docid, bemerkung)
values (p.posid, p.anzahl, p.gueltigab, p.docid, p.bemerkung);

By leveraging the Merge Command, you can efficiently handle both insertions and updates based on specified conditions, streamlining your data management processes in Oracle.

Update 2024 🔗

As of 2024, the use of PLSQL in Oracle databases has continued to evolve, with the Merge Command remaining a favored method for data merging. While simple insert-update statements are still viable, the Merge Command offers a more sophisticated approach to data manipulation.

While Oracle does not provide a direct way to merge data into a single table without using a join, the DUAL table workaround remains a viable solution. The syntax may have evolved over the years, but the core concept of using the Merge Command in PLSQL for data manipulation persists.

Looking for additional examples and guidance on Merge Commands and Joins in Oracle? There are numerous online resources available to assist you in optimizing your database queries and operations.

Conclusion 2025 🔗

In 2025, the PLSQL Merge Statement continues to be a reliable and efficient method for updating and inserting data in Oracle databases. Developers can benefit from its performance and elegance, making data management tasks more streamlined and effective. With the evolution of technology, the Merge Command remains a valuable tool in the Oracle developer’s toolkit, enhancing data manipulation capabilities and improving workflow efficiencies.

By utilizing the Merge Command in PLSQL, developers can ensure seamless data integration and management in their Oracle databases, propelling them towards greater success and productivity.

Remember, the Merge Command in PLSQL is a powerful tool that can simplify your data manipulation tasks and enhance your overall database performance. Stay updated with the latest advancements in PLSQL to leverage its full potential in your Oracle projects.