A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://linq2db.github.io/articles/sql/merge/Merge-API.html below:

Merge API | Linq To DB

Merge API

This API available since linq2db 1.9.0. It superseeds previous version of API with very limited functionality. For migration from old API check link below.

Supported Databases Introduction

Merge is an atomic operation to update table (target) content using other table (source). Merge API provides methods to build Merge command and execute it. Command could have following elements (availability depends on database engine, see [[support table|Merge-API-:-Background-Information-and-Providers-Support]] for more details):

Merge Operations

Merge operations could be splitted into three groups:

Each group of operations work with their own set of source and target records and could contain more than one operation. In this case each operation must have operation condition except last one, which could omit it and be applied to all remaining records. Operations within group must be ordered properly.

Example

You want to do following: update status of all orders in AwaitingConfirmation status to Confirmed and delete all orders with amount equal to 0. Your merge operation will look like:

db.Orders

    // start merge command
    .Merge()

    // use the same table for source
    .UsingTarget()

    // match on primary key columns
    .OnTargetKey()

    // first delete all records with 0 amount
    // we also can use source in condition because they reference the same record in our case
    .DeleteWhenMatchedAnd((target, source) => target.amount == 0)

    // for records, not handled by previous command, update records in AwaitingConfirmation status
    .UpdateWhenMatchedAnd(
        (target, source) => target.status == Status.AwaitingConfirmation,
        (target, source) => new Order() { status = Status.Confirmed })

    // send merge command to database
    .Merge();

In example above, delete and update operations belong to the same match group so their order is important. If you will put Update before Delete your merge command will do something else: it will update all orders in AwaitingConfirmation status and for remaining orders will remove those with 0 amount. After merge execution you could receive confirmed orders with 0 amount in Orders table.

Matched operations

Because those operations executed for records, present in both target and source, they have access to both records. There are two operations in this group (plus one non-standard operation for Oracle):

Not matched operations

Those operations executed for records, present only in source table, so they could access only target table properties. This group contains only one operation - Insert operation, which adds new record to target table.

Not matched by source operations

This is SQL Server-only extension, that allows to perform operations for records, present only in target table. This group contains same operations as Matched group with one distinction - operations could access only target record:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4