Grafana (V7) adding variable in table name
Asked Answered
K

2

6

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

Kolk answered 17/8, 2020 at 10:15 Comment(1)
did you solve this? Also, I assume that you looked at stackoverflow.com/questions/59792809 and that when you mention that you "enclose it in curly brackets" you do something like ${var}, right? did you try ${var:raw}?Soble
N
4

I found double square brackets works. e.g.

Rather than

select * from table_$variable_name;

use

select * from table_[[variable_name]];
Neomineomycin answered 18/10, 2022 at 16:42 Comment(1)
square brackets are deprecated. use select * from table_${variable_name} instead. See source here: grafana.com/docs/grafana/latest/dashboards/variables/…Enrique
C
2

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

Cassandra answered 26/2, 2021 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.