| Components | All | New | MacOS | Windows | Linux | iOS | ||||
| Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old | |
FM.ExecuteSQL.LastSQL
Queries last SQL command.
| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
| FM FMSQL | 7.4 | ✅ 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" ) ;
errorText = MBS( "FM.ExecuteSQL.LastErrorMessage" ) ;
sql = MBS( "FM.ExecuteSQL.LastSQL" ) ;
path = "/Users/cs/Desktop/sql.log" ;
r = MBS(
"Text.AppendTextFile" ;
"Error " &
errorCode &
": " &
errorText &
" while running " &
sql &
¶ &
¶ ;
path
)
] ;
""
)
)
] ;
""
)
// to be used after a MBS() call with SQL to log the error.
[
e = MBS( "IsError" ) ;
r = If(
e ;
Let(
[
errorCode = MBS( "FM.ExecuteSQL.LastError" ) ;
errorText = MBS( "FM.ExecuteSQL.LastErrorMessage" ) ;
sql = MBS( "FM.ExecuteSQL.LastSQL" ) ;
path = "/Users/cs/Desktop/sql.log" ;
r = MBS(
"Text.AppendTextFile" ;
"Error " &
errorCode &
": " &
errorText &
" while running " &
sql &
¶ &
¶ ;
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"; ""; sql);
errorCode = MBS( "FM.ExecuteSQL.LastError" );
lastSQL = MBS("FM.ExecuteSQL.LastSQL")
]; errorCode & ": " & lastSQL)
Example result: 8309: SELECT * FROM Test
sql = "SELECT * FROM Test";
r = MBS("FM.ExecuteFileSQL"; ""; sql);
errorCode = MBS( "FM.ExecuteSQL.LastError" );
lastSQL = MBS("FM.ExecuteSQL.LastSQL")
]; errorCode & ": " & lastSQL)
Example result: 8309: SELECT * FROM Test
Build a JSON after causing an error:
Let([
sql = "INSERT INTO Test (FirstName, Age) VALUES (?,?)";
r = MBS("FM.ExecuteFileSQL"; ""; sql; 9; 13; "Joe"; 23);
errorCode = MBS( "FM.ExecuteSQL.LastError" );
errorMessage = MBS("FM.ExecuteSQL.LastErrorMessage");
lastSQL = MBS("FM.ExecuteSQL.LastSQL");
params = MBS("FM.ExecuteSQL.LastParameters");
json = JSONSetElement ( "{}" ;
["Parameters"; params; JSONArray];
["ErrorCode"; errorCode; JSONNumber];
["SQL"; lastSQL; JSONString];
["ErrorMessage"; errorMessage; JSONString])
]; JSONFormatElements ( json ))
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 (?,?)" }
sql = "INSERT INTO Test (FirstName, Age) VALUES (?,?)";
r = MBS("FM.ExecuteFileSQL"; ""; sql; 9; 13; "Joe"; 23);
errorCode = MBS( "FM.ExecuteSQL.LastError" );
errorMessage = MBS("FM.ExecuteSQL.LastErrorMessage");
lastSQL = MBS("FM.ExecuteSQL.LastSQL");
params = MBS("FM.ExecuteSQL.LastParameters");
json = JSONSetElement ( "{}" ;
["Parameters"; params; JSONArray];
["ErrorCode"; errorCode; JSONNumber];
["SQL"; lastSQL; JSONString];
["ErrorMessage"; errorMessage; JSONString])
]; JSONFormatElements ( json ))
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 (?,?)" }
See also
- FM.ExecuteSQL.LastError
- FM.UpdateRecords4
- FM.UpdateRecords5
- JSON.DeleteRecord
- JSON.Import
- JSON.InsertOrUpdateRecord
- JSON.InsertRecord
- JSON.UpdateRecords
- Text.AppendTextFile
- XML.Import.Work
Release notes
- Version 7.4
- Added FM.ExecuteSQL.LastSQL function to query last executed SQL command.
Blog Entries
- SQL error checking improved
- MBS FileMaker Advent calendar - Door 13 - Last SQL
- Insert or update with JSON
- Import CSV with Matrix functions
- MBS FileMaker Plugin, version 7.4pr5
- MBS FileMaker Plugin Schulung auf Deutsch bei der FMK 2017
This function is free to use.
Created 11st September 2017, last changed 27th April 2026