SQL Query

The SQL Query action step allows you to write and run SQL scripts against SQL databases and Google spreadsheets. UI components' values and other variables can be added to the SQL query using the {{ui.input.value}} expression in the query body.

Action configuration

To run an SQL Query action, you will first need to select your SQL or Google Sheets data source or connect one, if you haven't done it yet. Then, you'll simply have to select the SQL Query step and specify your query.

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.

An example of a basic query with variables may be the following:

Using variables in a query

Here are a few other examples of using variables in an SQL QueryπŸ‘‡

  • If you're using dollar quoting in SQL queries, remember that SQL skips replacements inside such sections to avoid SQL injection risks.

  • You can add % to a variable value to make a wildcard search:

You don't need to put variables in quotes - they will be sent separately and quotes will be added automatically.

  • You can set default values if a variable is empty or not provided:

Check the action's Payload tab to see the query and variables sent to the database.

  • You can insert a query example:

  • You can insert a query with default values:

  • A multi-select array value can be used directly without the need to convert it to a string:

Date & Time variables

Sending dates to the query doesn't require any special formatting - you can send them just like this:

The JavaScript Date object will be automatically converted to the SQL date format - YYYY-MM-DD.

If your columns store both Date and Time and you only need to compare dates, wrap the Date column with the DATE() function:

You can also use the moment.js library to additionally convert the JavaScript Date to a specific format:

More info about using moment.js in UI Bakery you can find hereπŸ‘‡

Using JS libraries

Using JavaScript to generate queries

By default, all new data sources send the query and variables separately, so you will have to configure your data source to send the query as a single string.

This setting can be enabled on the Data source level. You need to go to the Data sources page and click the pencil icon next to the data source you want to edit. There, deselect the Convert SQL queries to prepared statements checkbox and update your data source.

After that, the queries will be compiled and sent as a single string, so you can use JavaScript to generate them, for example:

❗Now the quotes will NOT be added automatically - you will have to add them manually and make sure the query is valid:

Executing multiple insert queries

With the setting above disabled, you can execute multiple insert queries. This is how you can do thatπŸ‘‡

  1. First, create a JavaScript Code action that will take your data and generate the values of INSERT:

Depending on your data, this may result in a similar string:

  1. Now, add an SQL Query action as the second step with the rest of the INSERT statement.

It'll reference the {{data}} property (the result of the previous step execution) that has the generated values of the query.

Data transformation

If the database returns its data in a different format than expected for the components, you can modify it. For example, you can turn on the Transform result toggle in the action's settings or add a new JavaScript Code step to transform the data.

Here are some examples of possible data transformations:

  • Access an inner array object and map it to a new array:

  • Add a new key to the array of objects:

  • Filter an array of objects (short version):

Multiline version:

Last updated

Was this helpful?