Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 7.5   8.0   8.1   8.2   8.3   8.4   8.5   9.0   9.1   9.2    Statistic  


Execute the SQL Statement against the current FileMaker File.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
FM FMSQL 1.2 Yes Yes Yes Yes Yes
MBS( "FM.ExecuteSQL"; SQL Statement { ; Column Delimiter; Record Delimiter } )   More


Parameter Description Example value
SQL Statement SQL Statement as a Text string
Column Delimiter Optional
Columns Separator, only used with SELECT statement. Default is TAB
Record Delimiter Optional
Record Separator, only used with SELECT statement, Default is Return


Returns result of SQL expression or error.


Execute the SQL Statement against the current FileMaker File.
There must be an occurrence of the Table on the current tables' Graph. SELECT returns the the records in as a tabbed delimited text string. All other SQL commands return "".
Use FM.ExecuteSQL.LastError to check for errors.
String comparisons are Case Senstive! WHERE First_Name="todd" will not find records that have "Todd" in the first Name Field. You can wrap the field you are searching for with "lower" like this WHERE lower(First_Name)="todd" KNOWN LIMITATIONS Schema editing SQL functions are not supported in FileMaker 8 and 8.5.
Record Locking Errors are Not Reported! This means you must exercise caution when using UPDATE. CAUTION! This is a very powerful function! It is relying on very new and relatively untested resources made available by the release of FileMaker 8.
Seems like on FileMaker 11 you get LastError 8310 for any syntax error, like bad braces or missing quotes. You can put field names in double quotes and string values in single quotes.
Use FM.ExecuteSQLOnIdle for special commands which change database like ALTER TABLE.
Use FM.ExecuteSQL.LastErrorMessage and FM.ExecuteSQL.LastError to see error message.
Error 8309 means you did pass wrong data type, like string for a number.

If you need to pass parameters, please use FM.ExecuteFileSQL.

You can make queries against FILEMAKER_TABLES and FILEMAKER_FIELDS to query information about fields in your solution. The functions FM.QueryBaseTableNames, FM.QueryFieldsForBaseTableName, FM.QueryFieldsForTableName and FM.QueryTableNames provide simplified functions for some queries.

FILEMAKER_TABLES table contains:
1. TableName
2. TableID
3. BaseTableName
4. BaseFileName
5. ModCount

FILEMAKER_FIELDS table contains:
1. TableName
2. FieldName
3. FieldType (the SQL data type, not the FileMaker data type)
4. FieldID
5. FieldClass (Normal, Summary, Calculated)
6. FieldReps
7. ModCount


SELECT records

MBS("FM.ExecuteSQL";"select * from Contacts")

INSERT record

MBS("FM.ExecuteSQL";"INSERT INTO Contacts (FirstName, LastName) Values('Bob', 'Miller')")

Select records using delimiters

MBS("FM.ExecuteSQL";"select * from Contacts"; "#"; "|")

SELECT records with a Where clause

MBS("FM.ExecuteSQL"; "SELECT First_Name, Last_Name from Contacts WHERE ID_Contact_PK ='CN10013'")

Get list of fields in all tables:

MBS("FM.ExecuteSQL";"SELECT * FROM FileMaker_Fields")

Create Record with values via SQL

Set Variable [$r; Value:MBS( "FM.ExecuteSQL"; "INSERT INTO \"Export records\" (\"First Name\", \"Last Name\", \"Street\", \"Zip\", \"City\", \"Country\", \"Phone\") VALUES ('MyFirst', 'MyLast', 'MyStreet', 'MyZip', 'MyCity', 'MyCountry', 'MyPhone')")]
If [MBS("IsError")]
    Show Custom Dialog [MBS( "FM.ExecuteSQL.LastErrorMessage" )]
End If

Set value for all records of a table:

MBS( "FM.ExecuteSQL"; "UPDATE \"My Table\" SET \"My Field\" = 'Hello' ")

Set timestamp to today:

MBS( "FM.ExecuteSQL"; "UPDATE \"my Table\" SET \"Change Time\" = TODAY WHERE \"ID\" = '12345' ")

Update 5 fields:

MBS( "FM.ExecuteSQL"; "UPDATE \"my Table\" SET \"Number Field" = 1, \"Text Field" = 'test', \"Dest Field\" = \"Source Field\", \"Hour Field\" = CAST(HOUR(curtime) AS VARCHAR), \"Empty FIeld\" = NULL WHERE \"ID\" = '12345' ")

Select with passing variable as part of expression:

MBS("FM.ExecuteSQL"; "SELECT First_Name, Last_Name from Contacts WHERE ID_Contact_PK ='" & $Variable & "'")

Update date field:

MBS( "FM.ExecuteSQL"; "UPDATE myTable SET myDateField = CAST('2015-08-15' as DATE) " )

Delete all data in a table:

$Result = MBS( "FM.ExecuteSQL"; "DELETE FROM \"Prova\";")

Create an index:

MBS( "FM.ExecuteSQL"; "CREATE INDEX ON tablename.fieldname" )

Drop an index:

MBS( "FM.ExecuteSQL"; "DROP INDEX ON tablename.fieldname" )

Value in List of values:

MBS( "FM.ExecuteSQL"; "select * from \"Anlagen\" where \"Modell\" in ('test', 'abc')")

Delete all records from Table People:

# no where condition, so deletes all records!
MBS( "FM.ExecuteSQL"; "DELETE FROM \"People\"")

Query value of a repeating field with []:

MBS( "FM.ExecuteSQL"; "SELECT test[2] FROM Kontakte")

Set value of repeating field:

MBS( "FM.ExecuteSQL"; "UPDATE Kontakte SET test[2] = 'test'")

Query only first row:


Truncates table in FileMaker 15 or newer:

MBS( "FM.ExecuteSQL"; "TRUNCATE TABLE Kontakte")

Query field ID for a field with given name and table name:

MBS("FM.ExecuteSQL"; "SELECT FieldID FROM FileMaker_Fields WHERE FieldName='Vorname' and TableName='Kontakte' ")

Query repetition count of global field by name:

MBS("FM.ExecuteSQL"; "SELECT FieldReps FROM FILEMAKER_FIELDS WHERE FieldName='Wiederholen' ")

Query field name and repetition count for all fields in a table:

MBS("FM.ExecuteSQL"; "SELECT FieldReps, FieldName FROM FILEMAKER_FIELDS WHERE TableName='Aufgaben' AND FieldType like '%global%' ")

Query list of record IDs for a given table:


See also

Example Databases

Blog Entries

Created 18th August 2014, last changed 14th December 2018

FM.ExecuteFileSQLOnIdle   -   FM.ExecuteSQL.LastError

Feedback: Report problem or ask question.

MBS Xojo Plugins