Templates and variables
Template variables allow you to create dynamic, reusable dashboards. Instead of hard-coding values like table names or filter criteria, you can use variables that users can change from a drop-down menu.
For more information about variables in Grafana, refer to Templates and variables.
Supported variable types
You can use most Grafana variable types in dashboards that query SAP HANA. The Query type is backed by the SAP HANA data source and runs an SQL query to fetch values. The other types listed below are provided by Grafana and work regardless of the data source. The Ad hoc filters type isn’t supported by this data source.
Create a query variable
Query variables dynamically fetch values from your SAP HANA database using SQL queries.
To add a new SAP HANA query variable:
- From the Dashboard settings gear icon, click Variables.
- Click New variable.
- Select Query as the variable type.
- Select the SAP HANA data source.
- Enter your SQL query.
- Click Apply.
For general instructions, refer to Add a query variable.
Single column queries
The most common approach is to return a single column of distinct values.
Example:
The following query returns the distinct list of username from the users table:
SELECT DISTINCT("username") FROM "users"Key-value variable queries
You can create a variable with different display text and underlying values by returning two columns named __text and __value.
Example:
The following query uses host_name as the display text and host_id as the actual value:
SELECT host_name AS "__text", host_id AS "__value" FROM hosts_list_tableNote
The
__textcolumn value should be unique. If it’s not unique, the first value is used. Options in the drop-down have a friendly name as text and an ID as the value.
Query result behavior
The plugin handles query results as follows:
- 1 column: The column values become both the display text and the variable value.
- 2 columns: If columns are named
__textand__value, they’re used for display and value respectively. Otherwise, the first column is used for display and the second for value. - More than 2 columns: If columns named
__textand__valueexist, they’re used regardless of other columns. Otherwise, only the first column is used for both text and value.
Use variables in queries
You can use Grafana template variables anywhere in your SAP HANA queries. Variables are replaced with their current values when the query executes.
By default, the data source quotes variable values with single quotes, so a value inserts as 'london' and a multi-value selection inserts as 'london','perth'. To change this behavior, use a format modifier as described in Format variable values.
Note
The default single-quote formatting doesn’t escape single quotes within a value, so a value such as
O'Brienproduces invalid SQL. Avoid the default quoting for values that might contain single quotes.
Single value variables
Use the standard variable syntax to insert a single value:
-- Query with variable
SELECT * FROM "users" WHERE "city" = ${city}
-- Expands to (when city = 'london')
SELECT * FROM "users" WHERE "city" = 'london'Numeric variables
Variables also work with numeric fields. In this example, ${age} is a text box variable that accepts numbers:
-- Query with variable
SELECT * FROM "users" WHERE "age" > ${age}
-- Expands to (when age = 36)
SELECT * FROM "users" WHERE "age" > '36'Multi-value variables
If your variable returns multiple values (multi-select enabled), use it with the IN clause. Note the parentheses around the variable:
-- Query with multi-value variable
SELECT * FROM "users" WHERE "city" IN (${cities})
-- Expands to (when cities = london, perth, delhi)
SELECT * FROM "users" WHERE "city" IN ('london','perth','delhi')You can also use the shorthand notation without curly braces:
SELECT * FROM "users" WHERE "city" IN ($cities)Chained variables
You can build dependent variables where one query variable filters its results using the value of another. This creates cascading drop-downs, for example a schema variable that narrows a table variable.
In this example, a ${schema} variable is defined first, then a ${table} variable uses it:
-- Variable: schema
SELECT SCHEMA_NAME FROM SYS.SCHEMAS ORDER BY SCHEMA_NAME
-- Variable: table (depends on schema)
SELECT TABLE_NAME FROM SYS.TABLES WHERE SCHEMA_NAME IN (${schema}) ORDER BY TABLE_NAMEWhen you change the ${schema} selection, Grafana re-runs the table variable query and updates its options.
Variable syntax
The SAP HANA plugin supports multiple variable syntax formats:
Format variable values
By default, the data source inserts variable values with single quotes. When you need different formatting, for example an unquoted value or a custom delimiter, append a format modifier to the variable name.
For the full list of format options, refer to Advanced variable format options.


