SQL query
The SQL action allows you to write and run SQL scripts against SQL databases and Google Spreadsheets. UI component values and other variables can be added to the SQL query using the {{ui.input.value}}
expression in the query body.
Basic query with variables example:
Using variables
Everything that comes in the {{ }}
brackets is a JavaScript expression. This means that you can use variables, functions, and operators in the query body.
Adding '%' to a variable value to make it a wildcard search:
Note: you don't need to cover variables with quotes, variables will be sent separately, and quotes will be added automatically.
Default values can also be set in case a variable is not provided or empty:
Tip: check the Payload section to see the query and variables that are sent to the database:
Insert query example (note - no quotes around the variables):
Insert with default values:
Multi-select array value can be directly used with no need to convert it to a string:
Date&Time variables
Sending dates to the query doesn't require any special formatting:
JS Date object will be automatically converted to SQL date format which is YYYY-MM-DD
.
If your columns store date and time, and you only need to compare dates, wrap the date column with DATE()
function:
Use moment library to additionally convert JS Date to a specific format:
Use JavaScript to generate queries
By default, all new data sources send the query and variables separately, so you need to additionally configure the data source to send the query as a pure string.
To enable this setting on a Data Source level:
Go to the Data Sources Edit modal;
Uncheck the Convert SQL queries to prepared statements checkbox.
After that, the queries will be compiled and sent as a single string, so you can use JavaScript to generate the query:
Note: now the quotes will NOT be added automatically, so you need to add them manually and make sure the query is valid:
Data transformation
If the database returns its data in a different format than the components expect, enable transform result toggle or add a new Code step to transform the data.
Access an inner array object and map it to a new array:
Add a new key to the array of objects:
Filter array of objects, short version:
Multiline version:
Last updated