Parametrized queries

When there is a need to dynamically provide conditions (or any predicates, in fact) to the query, without having to rewrite it every time the conditions change, you know that you need query parameters.

Every query can be parametrized, and as we have already seen in filters, there is a convention for this.

Starting Redash v5, there are two ways to add a parameter to your query.

Either you add a keyword that is enclosed in double curly braces, for example, {{param_name}} to your query, and it will automatically introduce a parameter (exactly the same way as it was before v5 release).

Alternatively, you can click on the Add New Parameter button (shortcut Cmd / Ctrl + P).

Once Redash recognizes that you've added a parameter, a special param box will appear in the query editor:

For example, in the preceding query, a parameter named '{{ds_name}}' was added, and right after Redash recognized it (we didn't have to execute or save the query for this to happen; it happens as you type), it added the param box on the bottom left of the query editor.

Now, you can enter your desired value in that param box, click on Execute, and see the results based on the value you entered.

In the preceding query example, the match on the name field would be exact. If you want a partial match, you can modify the query to be the following (%%, which wraps the param name, acts as a wildcard in SQL):

select * from data_sources where name like '%{{ds_name}}%'
It is possible to use the same parameter several times in the query (if needed).

It is also possible to create more than one parameter in a single query.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.180.70