Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MERGE

Combines data from multiple sources or upserts records into a database table.

Syntax

MERGE table, data, key_column
MERGE table, data, key_columns, update_columns

Parameters

ParameterTypeDescription
tableStringTarget database table name
dataArray/ObjectData to merge (single record or array of records)
key_columnStringColumn(s) to match existing records
update_columnsArrayOptional specific columns to update on match

Description

MERGE performs an β€œupsert” operation: it inserts new records or updates existing ones based on matching key columns. This is useful for synchronizing data from external sources, importing bulk data, or maintaining data consistency.

Examples

Basic Merge (Single Record)

contact = #{
    email: "john@example.com",
    name: "John Smith",
    phone: "+1-555-0123"
}

MERGE "contacts", contact, "email"
TALK "Contact merged successfully"

Bulk Merge

new_products = GET "https://api.supplier.com/products"

MERGE "products", new_products, "sku"
TALK "Merged " + LEN(new_products) + " products"

Merge with Specific Update Columns

price_updates = [
    #{sku: "ABC123", price: 29.99},
    #{sku: "DEF456", price: 49.99},
    #{sku: "GHI789", price: 19.99}
]

MERGE "products", price_updates, "sku", ["price"]
TALK "Prices updated"

Composite Key Match

attendance = #{
    employee_id: "EMP001",
    date: TODAY(),
    status: "present",
    check_in: NOW()
}

MERGE "attendance", attendance, "employee_id,date"

Sync from External API

SET SCHEDULE "every 6 hours"

' Fetch latest data from CRM
customers = GET "https://crm.example.com/api/customers"

' Merge into local database
MERGE "customers", customers, "crm_id"

TALK "Synced " + LEN(customers) + " customer records"

Return Value

Returns an object with merge statistics:

PropertyDescription
insertedNumber of new records created
updatedNumber of existing records updated
unchangedNumber of records that matched but had no changes
totalTotal records processed
result = MERGE "products", data, "sku"
TALK "Inserted: " + result.inserted + ", Updated: " + result.updated

Sample Conversation

Behavior

On Match (Key Exists)

  • Updates all columns in the data (or only update_columns if specified)
  • Preserves columns not in the data
  • Updates updated_at timestamp if column exists

On No Match (New Record)

  • Inserts new row with all provided columns
  • Sets created_at timestamp if column exists

Common Patterns

Daily Data Import

SET SCHEDULE "every day at 2am"

data = GET "https://data.provider.com/daily-export"
result = MERGE "imported_data", data, "external_id"

IF result.inserted > 0 THEN
    SEND MAIL "admin@company.com", "Data Import", 
        "Imported " + result.inserted + " new records"
END IF

Inventory Sync

inventory = GET "https://warehouse.api/stock-levels"
MERGE "products", inventory, "sku", ["quantity", "last_restock"]

User Profile Updates

profile = #{
    user_id: current_user_id,
    preferences: user_preferences,
    last_active: NOW()
}
MERGE "user_profiles", profile, "user_id"

See Also

  • INSERT - Insert new records only
  • UPDATE - Update existing records only
  • SAVE - Simple data persistence
  • FIND - Query data before merging