FM.ExecuteSQL.LastSQL
---------------------

Queries last SQL command.

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

MBS( "FM.ExecuteSQL.LastSQL" ) ### Parameters

none

### Result

Returns text or error.

### Description

Queries last SQL command.  
This is for executing SQL in FileMaker and provides you the last SQL command as run by the plugin.  
### Examples

Custom function to log the last SQL error to a log file:

 Let(  
 \[  
 e = MBS ( "IsError" ) ;  
 r = If(  
 e ;  
 Let(  
 \[  
 errorCode = MBS ( "[FM.ExecuteSQL.LastError](FMExecuteSQLLastError.md)" ) ;  
 errorText = MBS ( "[FM.ExecuteSQL.LastErrorMessage](FMExecuteSQLLastErrorMessage.md)" ) ;  
 sql = MBS ( "FM.ExecuteSQL.LastSQL" ) ;  
 path = "/Users/cs/Desktop/sql.log" ;  
 r = MBS (  
 "[Text.AppendTextFile](TextAppendTextFile.md)" ;  
 "Error " &amp;  
 errorCode &amp;  
 ": " &amp;  
 errorText &amp;  
 " while running " &amp;  
 sql &amp;  
 ¶ &amp;  
 ¶ ;  
 path  
 )  
 \] ;  
 ""  
 )  
 )  
 \] ;  
 ""  
)  
  
// to be used after a MBS () call with SQL to log the error.  
Query last SQL after causing an error:

 Let(\[  
 sql = "SELECT \* FROM Test";  
 r = MBS ("[FM.ExecuteFileSQL](FMExecuteFileSQL.md)"; ""; sql);  
 errorCode = MBS ( "[FM.ExecuteSQL.LastError](FMExecuteSQLLastError.md)" );  
 lastSQL = MBS ("FM.ExecuteSQL.LastSQL")  
\]; errorCode &amp; ": " &amp; lastSQL)  
<small>  
Example result: 8309: SELECT \* FROM Test</small>Build a JSON after causing an error:

 Let(\[  
 sql = "INSERT INTO Test (FirstName, Age) VALUES (?,?)";  
 r = MBS ("[FM.ExecuteFileSQL](FMExecuteFileSQL.md)"; ""; sql; 9; 13; "Joe"; 23);  
  
 errorCode = MBS ( "[FM.ExecuteSQL.LastError](FMExecuteSQLLastError.md)" );  
 errorMessage = MBS ("[FM.ExecuteSQL.LastErrorMessage](FMExecuteSQLLastErrorMessage.md)");  
 lastSQL = MBS ("FM.ExecuteSQL.LastSQL");  
 params = MBS ("[FM.ExecuteSQL.LastParameters](FMExecuteSQLLastParameters.md)");  
  
 json = JSONSetElement ( "{}" ;   
 \["Parameters"; params; JSONArray\];   
 \["ErrorCode"; errorCode; JSONNumber\];   
 \["SQL"; lastSQL; JSONString\];   
 \["ErrorMessage"; errorMessage; JSONString\])  
\]; JSONFormatElements ( json ))  
<small>  
Example result:   
{ "ErrorCode" : 8309 , "ErrorMessage" : "ERROR: FQL0002/(1:12): The table named \\"Test\\" does not exist." , "Parameters" : \[ "Joe" , 23 \], "SQL" : "INSERT INTO Test (FirstName, Age) VALUES (?,?)" } </small>### See also

- [FM.ExecuteSQL.LastError](FMExecuteSQLLastError.md)
- [FM.UpdateRecords4](FMUpdateRecords4.md)
- [FM.UpdateRecords5](FMUpdateRecords5.md)
- [JSON.DeleteRecord](JSONDeleteRecord.md)
- [JSON.Import](JSONImport.md)
- [JSON.InsertOrUpdateRecord](JSONInsertOrUpdateRecord.md)
- [JSON.InsertRecord](JSONInsertRecord.md)
- [JSON.UpdateRecords](JSONUpdateRecords.md)
- [Text.AppendTextFile](TextAppendTextFile.md)
- [XML.Import.Work](XMLImportWork.md)

### Release notes

- **Version 7.4**
    - Added [FM.ExecuteSQL.LastSQL](http://www.mbsplugins.eu/FMExecuteSQLLastSQL.shtml) function to query last executed SQL command.

### Blog Entries

- [SQL error checking improved](https://www.mbsplugins.de/archive/2026-04-30/SQL_error_checking_improved/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Advent calendar - Door 13 - Last SQL](https://www.mbsplugins.de/archive/2025-12-13/MBS_FileMaker_Advent_calendar_/monkeybreadsoftware_blog_filemaker)
- [Insert or update with JSON](https://www.mbsplugins.de/archive/2024-08-28/Insert_or_update_with_JSON/monkeybreadsoftware_blog_filemaker)
- [Import CSV with Matrix functions](https://www.mbsplugins.de/archive/2020-11-20/Import_CSV_with_Matrix_functio/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 7.4pr5](https://www.mbsplugins.de/archive/2017-09-11/MBS_FileMaker_Plugin_version_7/monkeybreadsoftware_blog_filemaker)

This function is free to use.

Created 11st September 2017 , last changed 27th April 2026

  
[FM.ExecuteSQL.LastParameters](FMExecuteSQLLastParameters.md) - [FM.ExecuteSQLOnIdle](FMExecuteSQLOnIdle.md)

[HTML Version](FMExecuteSQLLastSQL.shtml)