Skip to content
Docs

Workflows

SQL Query

Read from your own PostgreSQL or MySQL database, and save the rows for the rest of the workflow.

SQL Query runs a read-only query on your own database and saves the rows – such as a customer’s balance, their last orders or a branch’s opening hours.

Important: SQL Query is only on the Enterprise plan.

Before you start

Connect your database. You can connect PostgreSQL and MySQL databases.

  1. In the sidebar, select your name, then Settings.
  2. Under Teamspace, select Database connections, then Connect database.
  3. Enter a Name, choose the Database type and save.
  4. Fill in the Credentials and Authentication. Turn on SSL/TLS if your database needs an encrypted connection.
  5. Add the IP address under Whitelist Proto proxy IP to your database’s allowlist, so Proto can reach it.
  6. Select Save, then Test connection.

Checkpoint: the connection’s Status says Connected.

Once it’s tested, a connection’s settings are locked. To change them, delete the connection and add it again.

Tip: connect with a database user that can only read the tables your workflows need.

Settings

The SQL Query panel, with Database connector, Get the full query from a variable and Query

SettingWhat it does
Database connectorWhich database to query. Only tested connections are listed.
QueryA SELECT query. Put values in as bind parameters – see Use values in a query.
Bind parametersThe value for each parameter in the query. Proto lists them as you write it.
Get the full query from a variableRun a whole query saved in a variable, such as query, instead. It must still be a SELECT.
Timeout after (seconds)How long to wait, from 30 to 60 seconds. 30 unless you change it.
Resulting variableRequired. Where to save the rows, such as query_results.

Use values in a query

Don’t put variables in the query itself. Use a bind parameter, and give it a value. Bind parameters keep the person’s answers from changing your query.

PostgreSQL – number the parameters $1, $2 and so on, with no gaps:

SELECT balance FROM accounts WHERE email = $1

Then, under Bind parameters, give $1 the value {email_address}.

MySQL – name each parameter:

SELECT balance FROM accounts WHERE email = %(email)s

Then, under Bind parameters, give email the value {email_address}.

What happens

Proto runs the query on your database, with the bind parameters filled in, and saves the rows in your Resulting variable. Then it runs the branch that matches the result. The person doesn’t see anything.

Branches

BranchRuns when
SuccessThe query ran. It may still have returned no rows.
ErrorThe query or connection failed, or a bind parameter uses a variable that doesn’t exist. What went wrong is in _sql_query_error.
Timed outThe query took longer than Timeout after (seconds).

Use the result

The rows are saved as a list, with each row’s columns by name:

ToWrite
Check a row came back, in a Branchquery_results
Show the first row’s balance, once you know there’s a row{query_results[0].balance}
Draw the rows as a chartquery_results in Send graph

Add LIMIT to your query – every row it returns is saved.

Good to know

  • Only SELECT queries run, and the connection itself is read-only.
  • Words that change data or settings – such as UPDATE, DELETE, SET, REPLACE, START, DO, END and COMMENT – are blocked anywhere in the query, outside quotes. That includes CASE … END, the REPLACE() function and columns with those names. In PostgreSQL, put such a column’s name in double quotes, such as "comment". MySQL has no way round it – use a view that renames the column.
  • SQL Query can’t be added to the Chat closed workflow.
  • Each run counts towards your interactions.

Stuck? Ask Advisor in the Proto dashboard, or contact us.

© Proto