Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
FM.SQL.InsertRecordsToSQL
Inserts records in SQL database from records in memory.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
FM FMSQL SQL | 6.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Parameters
Parameter | Description | Example | Flags |
---|---|---|---|
SQLref | The reference number returned by FM.SQL.Execute function. | $SQLRef | |
Connection | The connection reference number gained with SQL.NewConnection. | $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-1. Pass -1 to use all rows and the plugin will internally use RowCount-1. |
-1 | Optional |
Replace | Available in MBS FileMaker Plugin 7.1 or newer. 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 |
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.
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") ]
# Tell plugin where SQLite library is
Set Variable [ $result ; Value: MBS("SQL.InternalSQLiteLibrary.Activate") ]
# Tell plugin we want to use SQLite
Set Variable [ $result ; Value: MBS("SQL.SetClient"; $Connection; "SQLite") ]
# Connect to database in read/write/create mode. Creates new file if none exists.
Set Variable [ $result ; Value: MBS("SQL.Connect"; $Connection; "/Users/cs/Desktop/mydatabase.sqlite") ]
If [ $result = "OK" ]
# Query records in FileMaker
Set Variable [ $Records ; Value: MBS( "FM.SQL.Execute"; 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"; $Connection) ]
Set Variable [ $result2 ; Value: MBS( "FM.SQL.Release"; $Records ) ]
If [ $result ≠ "OK" ]
Show Custom Dialog [ "Error: " & $result ]
Else
Show Custom Dialog [ "Record exported." ]
End If
End If
Set Variable [ $result2 ; Value: MBS("SQL.FreeConnection"; $Connection) ]
See also
- FM.SQL.Execute
- FM.SQL.InsertRecords
- FM.SQL.Release
- Matrix.InsertRecordsToSQL
- SQL.Commit
- SQL.Connect
- SQL.Execute
- SQL.FreeConnection
- SQL.InsertRecords
- SQL.InternalSQLiteLibrary.Activate
Release notes
- Version 13.2
- Changed field quoting for FM.SQL.InsertRecordsToSQL and Matrix.InsertRecordsToSQL to handle non ANSI quotes for MySQL/MariaDB.
- Version 13.1
- Fixed FM.SQL.InsertRecords, FM.SQL.InsertRecordsToSQL, Matrix.InsertRecordsToSQL and Matrix.InsertRecords to quote the table name for the SQL used internally.
- Version 7.2
- Changed FM.SQL.InsertRecordsToSQL to no longer quote by default.
Example Databases
- SQL to other databases/ODBC Query
- SQL to other databases/SQL Export
- Third Party/FileMaker Snippet Storage
Blog Entries
- MBS FileMaker Plugin, version 13.2pr1
- Moving data from ODBC to FileMaker via script
- MBS FileMaker Plugin, version 13.1pr1
- Query FileMaker records as JSON
- Can FileMaker connect to a Microsoft Access database?
- MBS FileMaker Plugin, version 7.2pr1
- MBS FileMaker Plugin 6.4 for OS X/Windows
- MBS FileMaker Plugin, version 6.4pr3
FileMaker Magazin
This function checks for a license.
Created 1st August 2016, last changed 8th May 2023