2022-11-01 23:22:06 +08:00
---
aliases:
2022-12-10 00:36:04 +08:00
- ../../data-sources/mssql/template-variables/
2022-11-01 23:22:06 +08:00
description: Using template variables with Microsoft SQL Server in Grafana
keywords:
- grafana
- MSSQL
- Microsoft
- SQL
- Azure SQL Database
- templates
- variables
- queries
Explicitly set all front matter labels in the source files (#71548)
* Set every page to have defaults of 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration pages to have of 'Cloud', 'Enterprise', and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/enterprise-licensing pages to have 'Enterprise' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/organization-management pages to have 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/provisioning pages to have 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/recorded-queries pages to have labels cloud,enterprise
* Set administration/roles-and-permissions/access-control pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/stats-and-license pages to have labels cloud,enterprise
* Set alerting pages to have labels cloud,enterprise,oss
* Set breaking-changes pages to have labels cloud,enterprise,oss
* Set dashboards pages to have labels cloud,enterprise,oss
* Set datasources pages to have labels cloud,enterprise,oss
* Set explore pages to have labels cloud,enterprise,oss
* Set fundamentals pages to have labels cloud,enterprise,oss
* Set introduction/grafana-cloud pages to have labels cloud
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Fix introduction pages products
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set panels-visualizations pages to have labels cloud,enterprise,oss
* Set release-notes pages to have labels cloud,enterprise,oss
* Set search pages to have labels cloud,enterprise,oss
* Set setup-grafana/configure-security/audit-grafana pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/configure-authentication pages to have labels cloud,enterprise,oss
* Set setup-grafana/configure-security/configure-authentication/enhanced-ldap pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-authentication/saml pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-database-encryption/encrypt-secrets-using-hashicorp-key-vault pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-request-security pages to have labels cloud,enterprise,oss
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/configure-team-sync pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/export-logs pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set troubleshooting pages to have labels cloud,enterprise,oss
* Set whatsnew pages to have labels cloud,enterprise,oss
* Apply updated labels from review
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
---------
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
2023-07-18 16:10:12 +08:00
labels:
products:
- cloud
- enterprise
- oss
2022-11-01 23:22:06 +08:00
menuTitle: Template variables
title: Microsoft SQL Server template variables
weight: 400
2024-05-28 22:17:49 +08:00
refs:
variables:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/
variable-syntax-advanced-variable-format-options:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/variable-syntax/#advanced-variable-format-options
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/variable-syntax/#advanced-variable-format-options
add-template-variables:
- pattern: /docs/grafana/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/
- pattern: /docs/grafana-cloud/
destination: /docs/grafana/< GRAFANA_VERSION > /dashboards/variables/add-template-variables/
2022-11-01 23:22:06 +08:00
---
# Microsoft SQL Server template variables
Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
2025-08-01 02:36:09 +08:00
Grafana displays these variables in drop-down select boxes at the top of the dashboard to help you change the data displayed in your dashboard.
Grafana refers to such variables as **template variables** .
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
For general information on using variables in Grafana, refer to [Add variables ](ref:add-template-variables ).
For an introduction to templating and template variables, refer to [Templating ](ref:variables ) and [Add and manage variables ](ref:add-template-variables ).
2022-11-01 23:22:06 +08:00
## Query variable
2025-08-01 02:36:09 +08:00
A query variable in Grafana dynamically retrieves values from your data source using a query. With a query variable, you can write a SQL query that returns values such as measurement names, key names, or key values that are shown in a drop-down select box.
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
For example, the following query returns all values from the `hostname` column:
2022-11-01 23:22:06 +08:00
```sql
SELECT hostname FROM host
```
2025-08-01 02:36:09 +08:00
A query can return multiple columns, and Grafana automatically generates a list using the values from those columns. For example, the following query returns values from both the `hostname` and `hostname2` columns, which are included in the variable's drop-down list.
2022-11-01 23:22:06 +08:00
```sql
SELECT [host].[hostname], [other_host].[hostname2] FROM host JOIN other_host ON [host].[city] = [other_host].[city]
```
2025-08-01 02:36:09 +08:00
You can also create a key/value variable using a query that returns two columns named `__text` and `__value` .
- The `__text` column defines the label shown in the drop-down.
- The `__value` column defines the value passed to panel queries.
This is useful when you want to display a user-friendly label (like a hostname) but use a different underlying value (like an ID).
Note that the values in the `_text` column should be unique. If there are duplicates, Grafana uses only the first matching entry.
2022-11-01 23:22:06 +08:00
```sql
SELECT hostname __text, id __value FROM host
```
2025-08-01 02:36:09 +08:00
You can also create nested variables, where one variable depends on the value of another. For example, if you have a variable named `region` , you can configure a `hosts` variable to only show hosts from the selected region. If `region` is a multi-value variable, use the `IN` operator instead of `=` to match against multiple selected values.
2022-11-01 23:22:06 +08:00
```sql
SELECT hostname FROM host WHERE region IN ($region)
```
2025-08-01 02:36:09 +08:00
## Use variables in queries
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
Grafana automatically quotes template variable values only when the template variable is a `multi-value` .
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
When using a multi-value variable, use the `IN` comparison operator instead of `=` to match against multiple values.
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
Grafana supports two syntaxes for using variables in queries:
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
- **`$< varname > ` syntax**
Example with a template variable named `hostname` :
2022-11-01 23:22:06 +08:00
```sql
SELECT
atimestamp time,
aint value
FROM table
WHERE $__timeFilter(atimestamp) and hostname in($hostname)
ORDER BY atimestamp
```
2025-08-01 02:36:09 +08:00
- **`[[varname]]` syntax**
Example with a template variable named `hostname` :
2022-11-01 23:22:06 +08:00
```sql
SELECT
atimestamp as time,
aint as value
FROM table
WHERE $__timeFilter(atimestamp) and hostname in([[hostname]])
ORDER BY atimestamp
```
2025-08-01 02:36:09 +08:00
### Disable quoting for multi-value variables
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
By default, Grafana formats multi-value variables as a quoted, comma-separated string. For example, if `server01` and `server02` are selected, the result will be `'server01'` , `'server02'` . To disable quoting, use the `csv` formatting option for variables:
```text
${servers:csv}
```
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
This outputs the values as an unquoted comma-separated list.
2022-11-01 23:22:06 +08:00
2025-08-01 02:36:09 +08:00
Refer to [Advanced variable format options ](ref:variable-syntax-advanced-variable-format-options ) for additional information.