Use a query from query library

I have enabled dynamic querying in my resource. Is there a way to use a query from query library in a SQL statement?

select * from {{query}}? I could not find documentation on this just a few questions and the answers just speak about dynamic querying but no real example.

Hey There!

Thanks for reaching out about using queries from the query library within SQL statements in Retool. I'd be happy to clarify this functionality.

While dynamic querying offers flexibility, it's not currently possible to directly embed queries from the library within SQL statements using the {{query}} syntax. Retool's query library is designed to store and manage reusable queries, but they're not directly interchangeable with SQL syntax elements.

Alternative Approaches:

Here are a couple of effective ways to achieve similar outcomes:

1. Separate Queries and Data Combination:

- Execute the query from the library independently to retrieve the desired data.

- Store the results in a variable or state.

- Construct a separate SQL query that joins or references this stored data as needed.

2. Utilize Retool's Query Component:

- Write a JavaScript query component that:

- Fetches data using the library query.

- Constructs the desired SQL statement dynamically based on the fetched data.

- Executes the SQL statement using Retool's query functionality.

Here's an example of what the JavaScript code could look like in your Query Component:

// Fetch data from query library

const queryData = Retool.queries.runQuery("mySavedQuery");
// Construct dynamic SQL statement
const sql = `SELECT * FROM myTable WHERE id IN (${ =>",")})`;
// Run the SQL query
const queryResults = Retool.db.query(sql);
return queryResults

I hope this helps!