# MERGE Combines data from multiple sources or upserts records into a database table. ## Syntax ```basic MERGE table, data, key_column MERGE table, data, key_columns, update_columns ``` ## Parameters | Parameter | Type | Description | |-----------|------|-------------| | `table` | String | Target database table name | | `data` | Array/Object | Data to merge (single record or array of records) | | `key_column` | String | Column(s) to match existing records | | `update_columns` | Array | Optional 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) ```basic contact = #{ email: "john@example.com", name: "John Smith", phone: "+1-555-0123" } MERGE "contacts", contact, "email" TALK "Contact merged successfully" ``` ### Bulk Merge ```basic new_products = GET "https://api.supplier.com/products" MERGE "products", new_products, "sku" TALK "Merged " + LEN(new_products) + " products" ``` ### Merge with Specific Update Columns ```basic 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 ```basic attendance = #{ employee_id: "EMP001", date: TODAY(), status: "present", check_in: NOW() } MERGE "attendance", attendance, "employee_id,date" ``` ### Sync from External API ```basic 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: | Property | Description | |----------|-------------| | `inserted` | Number of new records created | | `updated` | Number of existing records updated | | `unchanged` | Number of records that matched but had no changes | | `total` | Total records processed | ```basic result = MERGE "products", data, "sku" TALK "Inserted: " + result.inserted + ", Updated: " + result.updated ``` ## Sample Conversation