bouvetIsle
bouvetIsle

Reputation: 113

Grafana (V7) adding variable in table name

I need to be able to use variables in table names - I basically have the same set of tables used for different types of data, so I would like to just have one dashboard and swapping between all types instead of always having to set up multiple identical dashboards.

My query is something like:

select * from table_$variable_name;

Where my list of possible variable is something like cat, dog, bird

I can seem to make this work, if I only put the variable as shown above I get the following error

Error 1146: Table 'table_$variable_name' doesn't exist

If I enclose it in curly brackets, I get this error instead.

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{bird}' at line 1

(i.e. with the selected variable actually being visible this time)

I'm not sure if the issue is having underscores in the table names, I tried putting underscores around my variables too to check and I had no luck with that.

Another thing I tried was gradually adding on to the table name, so e.g. select * from table_$variable; Still returns an error, but I can see the table name starting to form correctly Error 1146: Table 'table_bird_' doesn't exist

However, as soon as I add another underscore, the variable is not picked up abymore ```Error 1146: Table 'table_$variable_' doesn't exist``

I'm sure it's something silly I am missing in the syntax of the query - anyone has any suggestions?

Using this https://grafana.com/docs/grafana/latest/variables/templates-and-variables/ for reference

Upvotes: 6

Views: 2876

Answers (2)

Shane Gannon
Shane Gannon

Reputation: 7708

I found double square brackets works. e.g.

Rather than

select * from table_$variable_name;

use

select * from table_[[variable_name]];

Upvotes: 4

guo
guo

Reputation: 293

As @arturomp suggests, use ${var:raw} At least in my case, this was the solution that worked.

Upvotes: 2

Related Questions