FM.SQL.InsertOrUpdateRecords
----------------------------

Insert or updates a lot of records queried from a database.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [FM](component_FM.md) [FMSQL](component_FMSQL.md) | [16.1](newinversion161.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "FM.SQL.InsertOrUpdateRecords"; SQLref; InsertFileName; InsertTableName; FieldNames; KeyFieldMapping ) 

**MBS**( **"FM.SQL.InsertOrUpdateRecords";** /\* Insert or updates a lot of records queried from a database. \*/  
**$SQLref**; /\* The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. \*/   
**$InsertFileName**; /\* The file name of where the insert table is inside. Can be empty to look for the table in all files.e.g. Get(FileName) \*/   
**$InsertTableName**; /\* The name of the table to insert record into.  
Can be ID of table, so we lookup name by ID.   
Can be result of GetFieldName() function as we remove field name automatically.e.g. "Assets" \*/   
**$FieldNames**; /\* A list of field names for the insert.  
Empty entries in the list are ignored.  
  
Can be ID of field, so we lookup name by ID.   
Can be result of GetFieldName() function as we remove table name automatically.e.g. "Model" \*/   
**$KeyFieldMapping**) /\* The list of field names with index to map keys.  
Please specify a list of key name=key index.  
The key name is the name of the field in the insert table and the index gives zero-based position in result set from SELECT query in 6th parameter.  
  
Field order in source and destination does not need to be the same.e.g. ID=0 \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| SQLref | The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. | $SQLRef |
| InsertFileName | The file name of where the insert table is inside. Can be empty to look for the table in all files. | Get(FileName) |
| InsertTableName | The name of the table to insert record into.   Can be ID of table, so we lookup name by ID.    Can be result of GetFieldName() function as we remove field name automatically. | "Assets" |
| FieldNames | A list of field names for the insert.   Empty entries in the list are ignored.      Can be ID of field, so we lookup name by ID.    Can be result of GetFieldName() function as we remove table name automatically. | "Model" |
| KeyFieldMapping | The list of field names with index to map keys.   Please specify a list of key name=key index.   The key name is the name of the field in the insert table and the index gives zero-based position in result set from SELECT query in 6th parameter.      Field order in source and destination does not need to be the same. | ID=0 |

### Result

Returns OK or error.

### Description

Insert or updates a lot of records queried from a database.  
Similar to [FM.InsertOrUpdateRecordQuery](FMInsertOrUpdateRecordQuery.md), but taking records from a stored SQL result.  
  
Copy can be from one database to other or within same database. Also from one table to other or within a table. Due to passing in new field names, you can even rearrange values from one column to other while copying. In the SQL you can use functions for sums or join data from several tables together to fill a new table. Filename parameters can be empty.  
Use [FM.ExecuteSQL.LastErrorMessage](FMExecuteSQLLastErrorMessage.md) and [FM.ExecuteSQL.LastError](FMExecuteSQLLastError.md) to see error message.  
Reports an error if field name list doesn't match column count of the stored SQL result.  
  
Please remove from SQL statement and field names all fields which are globals or unstored calculations.  
  
For inserting records from other database (e.g. MySQL), please use [SQL.InsertRecords](SQLInsertRecords.md) function.  
  
You can specify fields and table via IDs and the plugin will lookup them for you at runtime. Table IDs and table names must be unique across all open files for this to work correctly.  
  
You can use GetFieldName() function to query field names and the plugin removes the table prefix with :: in the field name before passing it to SQL engine.  
### Examples

Try the insert and update:

 # Load records into a stored SQL result and insert/update into other table   
Set Variable \[ $SelectSQL ; Value: "SELECT PrimaryKey, TextField, NumberField, DateField, TimeField, TimestampField, ContainerField, Description FROM Source" \]  
Set Variable \[ $InsertFields ; Value: "Key¶TextField¶NumberField¶DateField¶TimeField¶TimestampField¶ContainerField¶Description" \]  
\#   
Set Variable \[ $sql ; Value: MBS ("[FM.SQL.Execute](FMSQLExecute.md)"; Get(FileName); $SelectSQL ) \]  
\#   
If \[ MBS ("IsError") \]  
 Show Custom Dialog \[ "Error: " &amp; $sql \]  
Else  
 Set Variable \[ $result ; Value: MBS ("FM.SQL.InsertOrUpdateRecords"; $sql ; Get(FileName); "Dest"; $InsertFields ; "Key=0") \]  
 If \[ MBS ("IsError") \]  
 Show Custom Dialog \[ "Error: " &amp; $result \]  
 Else  
 Show Custom Dialog \[ "Records updated." ; $result \]  
 End If  
End If  
\#   
Set Variable \[ $r ; Value: MBS ("[FM.SQL.Release](FMSQLRelease.md)"; $sql ) \]  
### See also

- [FM.InsertOrUpdateRecord](FMInsertOrUpdateRecord.md)
- [FM.InsertOrUpdateRecord2](FMInsertOrUpdateRecord2.md)
- [FM.InsertOrUpdateRecord3](FMInsertOrUpdateRecord3.md)
- [FM.InsertOrUpdateRecord4](FMInsertOrUpdateRecord4.md)
- [FM.InsertOrUpdateRecord5](FMInsertOrUpdateRecord5.md)
- [FM.SQL.InsertOrUpdateRecordsToSQL](FMSQLInsertOrUpdateRecordsToSQL.md)
- [FM.SQL.Release](FMSQLRelease.md)
- [IsError](IsError.md)
- [JSON.InsertOrUpdateRecord](JSONInsertOrUpdateRecord.md)
- [SQL.Execute](SQLExecute.md)

### Release notes

- **Version 16.1**
    - Added [FM.SQL.InsertOrUpdateRecords](https://www.mbsplugins.eu/FMSQLInsertOrUpdateRecords.shtml) and [Matrix.InsertOrUpdateRecords](https://www.mbsplugins.eu/MatrixInsertOrUpdateRecords.shtml) functions.
    - Added [FM.SQL.InsertOrUpdateRecordsToSQL](https://www.mbsplugins.eu/FMSQLInsertOrUpdateRecordsToSQL.shtml) and [Matrix.InsertOrUpdateRecordsToSQL](https://www.mbsplugins.eu/MatrixInsertOrUpdateRecordsToSQL.shtml) functions.

### Example Databases

- [SQL in FileMaker/Insert or Update Tests](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20in%20FileMaker/Insert%20or%20Update%20Tests.shtml#4ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin 16.1 News](https://www.mbsplugins.de/archive/2026-03-31/MBS_FileMaker_Plugin_161_News/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 16.1pr1](https://www.mbsplugins.de/archive/2026-02-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 23th January 2026 , last changed 12nd February 2026

  
[FM.SQL.FieldType](FMSQLFieldType.md) - [FM.SQL.InsertOrUpdateRecordsToSQL](FMSQLInsertOrUpdateRecordsToSQL.md)

[HTML Version](FMSQLInsertOrUpdateRecords.shtml)