SQL Functions
Custom functions for SQL operations in FileMaker solutions.
Introduction
These SQL custom functions simplify working with ExecuteSQL in FileMaker by providing consistent and reliable methods for constructing SQL statements and handling field and table names. They help overcome common challenges when working with SQL in FileMaker such as properly quoting table and field names, and building dynamic queries.
Related Functions
These SQL functions are used by other utility functions:
lookupFieldValue- A simplified lookup function that uses SQL.GetColumn to retrieve a single value
Functions
SQL.GetColumn(columnField; whereField; whereValue)
Description: Retrieves a single column of data using ExecuteSQL based on a simple WHERE clause with one condition. This function handles all the SQL statement construction and properly escapes field and table names.
Parameters:
columnField: The field to return values fromwhereField: The field to use in the WHERE clause conditionwhereValue: The value to match in the WHERE clause
Dependencies:
SQL.GetFieldName- For proper field name formattingSQL.GetTableName- For extracting table nameSQL.GetColumnStatement- For constructing the SQL statement
Return:
- The result of the SQL query (list of values from the specified column)
Example:
SQL.GetColumn2Fields(columnField; whereField; whereValue; whereField2; whereValue2)
Description:
Retrieves a single column of data using ExecuteSQL based on a WHERE clause with two conditions. This extends SQL.GetColumn by adding a second condition with AND logic.
Parameters:
columnField: The field to return values fromwhereField: The first field to use in the WHERE clausewhereValue: The value to match in the first conditionwhereField2: The second field to use in the WHERE clausewhereValue2: The value to match in the second condition
Dependencies:
SQL.GetFieldName- For proper field name formattingSQL.GetTableName- For extracting table name
Return:
- The result of the SQL query (list of values from the specified column)
Example:
SQL.GetColumnStatement(theField; whereField; whereValue)
Description: Generates a SQL SELECT statement for retrieving a single column with a simple WHERE clause. This function is primarily used by the other SQL functions but can be useful when you need to customize the SQL statement further.
Parameters:
theField: The field to return values fromwhereField: The field to use in the WHERE clausewhereValue: The value or parameter placeholder for the WHERE clause
Dependencies:
SQL.GetFieldName- For proper field name formattingSQL.GetTableName- For extracting table name
Return:
- A formatted SQL SELECT statement
Example:
SQL.GetFieldName(field)
Description: Properly formats a FileMaker field reference for use in SQL statements by adding double quotes around field names. This is crucial for fields with spaces or special characters.
Parameters:
field: The field reference to format
Return:
- A properly quoted field name for SQL statements
Example:
SQL.GetTableName(field)
Description: Extracts and properly formats the table name from a field reference for use in SQL statements. Handles the process of parsing the field name and adding appropriate quotes.
Parameters:
field: The field reference to extract table name from
Return:
- A properly quoted table name for SQL statements
Example:
Usage Tips
-
Parameter Placeholders: Use "?" as the
whereValuewhen constructing statements that will be used with variable parameters to prevent SQL injection vulnerabilities. -
Multiple Conditions: Use
SQL.GetColumn2Fieldswhen you need to filter by two criteria. For more complex queries, consider usingSQL.GetColumnStatementas a starting point and adding additional conditions. -
Field Naming: These functions properly handle quoted field and table names, allowing you to reference fields with spaces or special characters without errors.
-
Performance: When retrieving large datasets, consider adding additional query conditions to limit the result set size.