FM.SQL.InsertRecordsToSQL
-------------------------

Inserts records in SQL database from records in memory.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [FM](component_FM.md) [FMSQL](component_FMSQL.md) [SQL](component_SQL.md) | [6.4](newinversion64.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "FM.SQL.InsertRecordsToSQL"; SQLref; Connection; InsertTableName; FieldNames { ; StartRow; EndRow; Replace } ) 

**MBS**( **"FM.SQL.InsertRecordsToSQL";** /\* Inserts records in SQL database from records in memory. \*/  
**$SQLref**; /\* The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. \*/   
**$Connection**; /\* The connection reference number gained with [SQL.NewConnection](SQLNewConnection.md). \*/   
**$InsertTableName**; /\* The name of the table to insert record into.e.g. "Assets" \*/   
**$FieldNames**; /\* A list of field names for the insert.   
Empty entries in the list are ignored.e.g. "Model¶Names" \*/   
**$StartRow**; /\* Optional; The index of the start row.  
First row is 0.  
Pass -1 or nothing for default which is starting with row 0.e.g. -1 \*/   
**$EndRow**; /\* Optional; The index of the end row.  
First row is 0. Last Row would be [FM.SQL.RowCount](FMSQLRowCount.md)-1.  
Pass -1 to use all rows and the plugin will internally use RowCount-1.e.g. -1 \*/   
**$Replace**) /\* Optional; Pass for regular INSERT statements.  
Set to 1 to get REPLACE statements, which overwrite existing entries.  
Currently only available for MySQL and databases with same syntax.e.g. 0 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| SQLref | The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. | $SQLRef |  |
| Connection | The connection reference number gained with [SQL.NewConnection](SQLNewConnection.md). | $Connection |  |
| InsertTableName | The name of the table to insert record into. | "Assets" |  |
| FieldNames | A list of field names for the insert.    Empty entries in the list are ignored. | "Model¶Names" |  |
| StartRow | The index of the start row.   First row is 0.   Pass -1 or nothing for default which is starting with row 0. | -1 | Optional |
| EndRow | The index of the end row.   First row is 0. Last Row would be [FM.SQL.RowCount](FMSQLRowCount.md)-1.   Pass -1 to use all rows and the plugin will internally use RowCount-1. | -1 | Optional |
| Replace | Pass for regular INSERT statements.   Set to 1 to get REPLACE statements, which overwrite existing entries.   Currently only available for MySQL and databases with same syntax. | 0 | Optional      Added in version **7.1**. |

### Result

Returns OK or error.

### Description

Inserts records in SQL database from records in memory.  
This function allows to easily copy a lot of records from a query in FileMaker database into a SQL database.  
Please provide field names in other table. The order has to match those in the record set.  
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.   
Reports an error if field name list doesn't match column count of the query result.  
Function will fail if data types in source and dest fields do not match for assignment. e.g. date and time fields will not work, only timestamp fields.  
  
For insert within FileMaker, please use [FM.SQL.InsertRecords](FMSQLInsertRecords.md).  
  
Please don't overload the function, so better work in blocks of e.g. 1000 rows at a time.  
### Examples

Insert Records to SQL database:

 Set Variable \[$InsertResult ; Value:MBS ( "FM.SQL.InsertRecordsToSQL"; $Records ; $connection ; $BaseTableName ; $FieldNames )\]  
Copy records to SQLite:

 Set Variable \[ $Connection ; Value: MBS ("[SQL.NewConnection](SQLNewConnection.md)") \]   
\# Tell plugin where SQLite library is   
Set Variable \[ $result ; Value: MBS ("[SQL.InternalSQLiteLibrary.Activate](SQLInternalSQLiteLibraryActivate.md)") \]   
\# Tell plugin we want to use SQLite   
Set Variable \[ $result ; Value: MBS ("[SQL.SetClient](SQLSetClient.md)"; $Connection ; "SQLite") \]   
\# Connect to database in read/write/create mode. Creates new file if none exists.   
Set Variable \[ $result ; Value: MBS ("[SQL.Connect](SQLConnect.md)"; $Connection ; "/Users/cs/Desktop/mydatabase.sqlite") \]   
If \[ $result = "OK" \]   
 # Query records in FileMaker  
 Set Variable \[ $Records ; Value: MBS ( "[FM.SQL.Execute](FMSQLExecute.md)"; Get(FileName); "SELECT FirstName, LastName, Birthday FROM People") \]   
 # Transfer to other database  
 Set Variable \[ $r ; Value: MBS ( "FM.SQL.InsertRecordsToSQL"; $Records ; $Connection ; "People"; "FirstName¶LastName¶Birthday") \]   
 # Cleanup  
 Set Variable \[ $result2 ; Value: MBS ("[SQL.Commit](SQLCommit.md)"; $Connection ) \]   
 Set Variable \[ $result2 ; Value: MBS ( "[FM.SQL.Release](FMSQLRelease.md)"; $Records ) \]   
 If \[ $result ≠ "OK" \]   
 Show Custom Dialog \[ "Error: " &amp; $result \]   
 Else  
 Show Custom Dialog \[ "Record exported." \]   
 End If  
End If  
Set Variable \[ $result2 ; Value: MBS ("[SQL.FreeConnection](SQLFreeConnection.md)"; $Connection ) \]  
### See also

- [FM.SQL.Execute](FMSQLExecute.md)
- [FM.SQL.InsertRecords](FMSQLInsertRecords.md)
- [FM.SQL.Release](FMSQLRelease.md)
- [Matrix.InsertRecordsToSQL](MatrixInsertRecordsToSQL.md)
- [SQL.Commit](SQLCommit.md)
- [SQL.Connect](SQLConnect.md)
- [SQL.Execute](SQLExecute.md)
- [SQL.FreeConnection](SQLFreeConnection.md)
- [SQL.InsertRecords](SQLInsertRecords.md)
- [SQL.InternalSQLiteLibrary.Activate](SQLInternalSQLiteLibraryActivate.md)

### Release notes

- **Version 16.1**
    - Improved [Matrix.InsertRecordsToSQL](https://www.mbsplugins.eu/MatrixInsertRecordsToSQL.shtml) and [FM.SQL.InsertRecordsToSQL](https://www.mbsplugins.eu/FMSQLInsertRecordsToSQL.shtml) functions to pass NULL if a number or date field is empty. Otherwise SQL would complain about passing invalid date or number.
    - Rewrote [FM.SQL.InsertRecordsToSQL](https://www.mbsplugins.eu/FMSQLInsertRecordsToSQL.shtml) and [Matrix.InsertRecordsToSQL](https://www.mbsplugins.eu/MatrixInsertRecordsToSQL.shtml) functions to use same code internal.
- **Version 16.0**
    - Changed [FM.SQL.InsertRecordsToSQL](https://www.mbsplugins.eu/FMSQLInsertRecordsToSQL.shtml) and related functions to use backtick to quote table names for ODBC connections.
- **Version 13.2**
    - Changed field quoting for [FM.SQL.InsertRecordsToSQL](https://www.mbsplugins.eu/FMSQLInsertRecordsToSQL.shtml) and [Matrix.InsertRecordsToSQL](https://www.mbsplugins.eu/MatrixInsertRecordsToSQL.shtml) to handle non ANSI quotes for MySQL/MariaDB.
- **Version 13.1**
    - Fixed [FM.SQL.InsertRecords](https://www.mbsplugins.eu/FMSQLInsertRecords.shtml), [FM.SQL.InsertRecordsToSQL](https://www.mbsplugins.eu/FMSQLInsertRecordsToSQL.shtml), [Matrix.InsertRecordsToSQL](https://www.mbsplugins.eu/MatrixInsertRecordsToSQL.shtml) and [Matrix.InsertRecords](https://www.mbsplugins.eu/MatrixInsertRecords.shtml) to quote the table name for the SQL used internally.
- **Version 7.2**
    - Changed [FM.SQL.InsertRecordsToSQL](http://www.mbsplugins.eu/FMSQLInsertRecordsToSQL.shtml) to no longer quote by default.

### 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#18ScriptAnchor_)
- [SQL to other databases/ODBC Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/ODBC%20Query.shtml#5ScriptAnchor_)
- [SQL to other databases/SQL Export](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/SQL%20Export.shtml#1ScriptAnchor_)
- [SQL to other databases/SQLite fun](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/SQL%20to%20other%20databases/SQLite%20fun.shtml#11ScriptAnchor_)
- [Third Party/FileMaker Snippet Storage](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Third%20Party/FileMaker%20Snippet%20Storage.shtml#30CustomFunctionAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 16.1pr1](https://www.mbsplugins.de/archive/2026-02-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 15.6pr1](https://www.mbsplugins.de/archive/2025-12-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 13.2pr1](https://www.mbsplugins.de/archive/2023-04-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Moving data from ODBC to FileMaker via script](https://www.mbsplugins.de/archive/2023-02-12/Moving_data_from_ODBC_to_FileM/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 13.1pr1](https://www.mbsplugins.de/archive/2023-02-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Query FileMaker records as JSON](https://www.mbsplugins.de/archive/2020-04-03/Query_FileMaker_records_as_JSO/monkeybreadsoftware_blog_filemaker)
- [Can FileMaker connect to a Microsoft Access database?](https://www.mbsplugins.de/archive/2019-12-29/Can_FileMaker_connect_to_a_Mic/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 7.2pr1](https://www.mbsplugins.de/archive/2017-04-03/MBS_FileMaker_Plugin_version_7/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 6.4 for OS X/Windows](https://www.mbsplugins.de/archive/2016-09-27/MBS_FileMaker_Plugin_64_for_OS/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 6.4pr3](https://www.mbsplugins.de/archive/2016-08-08/MBS_FileMaker_Plugin_version_6/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 2/2022, Seite 29](https://filemaker-magazin.de/neuigkeit/4174-Appetithappen-FMM_202202)
- [Ausgabe 3/2018, Seite 34](https://filemaker-magazin.de/neuigkeit/3953-Appetithappen-FMM_201803)

This function checks for a license.

Created 1st August 2016 , last changed 8th May 2023

  
[FM.SQL.InsertRecords](FMSQLInsertRecords.md) - [FM.SQL.JSONColumn](FMSQLJSONColumn.md)

[HTML Version](FMSQLInsertRecordsToSQL.shtml)