ExecuteSQL
 
Purpose 
Executes an SQL query statement for the specified table occurrence in a FileMaker Pro file.
Format 
ExecuteSQL(sqlQuery; fieldSeparator; rowSeparator {;arguments...})
Parameters 
sqlQuery - an SQL SELECT statement. The statement can include a Union clause that combines the results of two queries. The statement can contain programmatically generated SQL (dynamic parameters) that indicate where optional arguments are to be used in the query. Use the question mark character (?) to specify a dynamic parameter.
fieldSeparator - the character string used as a separator between fields in the result. If an empty string is specified, the separator is a comma. The field separator is not displayed after the last field in the result.
rowSeparator - the character string used as a separator between records in the result. If an empty string is specified, the separator is a carriage return. The row separator is not displayed after the last row in the result.
arguments- one or more expressions that are evaluated and used as values for the dynamic parameters in the query statement.
Data type returned 
text
Originated in 
FileMaker Pro 12.0
Description 
ExecuteSQL enables you to execute SQL SELECT statements containing dynamic parameters to safely query FileMaker Pro databases in order to avoid security vulnerabilities through injection attacks.
ExecuteSQL does not recognize relationships created in FileMaker Pro, which gives you flexibility to define relationships in SQL statements and retrieve data from any table, independent of the layout context.
ExecuteSQL cannot be used with SQL statements that modify data or the database schema (such as the Insert Into or Delete Table commands).
If an error occurs during query parsing or execution, FileMaker Pro returns ?.
Notes
 •
 •
 •
 •
 •
Example 1 
Suppose a database contains two tables, Employees and Salaries, which are related through the EmpID field.
List displaying Employees and Salaries
Note  The Empoyees::EmpID, Salaries::EmpID, and Salaries::Salary fields are number fields.
You can use the ExecuteSQL function to return a field value from a specific record without changing the current record or modifying the found set.
ExecuteSQL ( "SELECT Department FROM Employees WHERE EmpID = 1" ) returns Development regardless of the current record, found set, or layout.
Example 2 
Suppose you want to add a field to the Employees table to display the percentage of an employee’s salary relative to the total salaries in a department. Though you could use a calculation in FileMaker Pro to generate this value, you can use the ExecuteSQL function to specify this query using dynamic parameters.
Define a calculation field in the Employees table, then use the ExecuteSQL function to specify the following query statement:
Round ( 100 * Salaries::Salary / ExecuteSQL ( “select sum ( S.salary ) from Employees E join Salaries S on E.EmpID = S.EmpID where E.Department = ?”; “”; “”; Employees::Department ) ; 2 )
On each employee record, the calculation field displays the percentage of the employee's salary relative to the sum of the salaries for the employee's department. For example, the record with a Last Name of "Smith" returns 52.97, and the record with a Last Name of "Mehmet" returns 100.
Related topics 
Functions reference (category list)
Functions reference (alphabetical list)
About formulas
About functions
Defining calculation fields
Using operators in formulas