mirror of
https://github.com/grafana/grafana.git
synced 2025-09-18 15:33:05 +08:00

* Docs: Revise data source index * Docs: Consolidate data source administration docs * Docs: Revise panels docs related to data sources * Docs: Revise Alertmanager data source * Docs: Reorganize AWS CloudWatch data source docs * Docs: Reorganize Azure Monitor data source docs * Docs: Move azuremonitor to azure-monitor * Docs: Revise Elasticsearch docs * Docs: Move Elasticsearch index into bundle * Docs: Revise GCM docs * Docs: Revise Graphite docs * Docs: Move Graphite index into bundle * Docs: Revise InfluxDB docs * Docs: Revise Jaeger docs * Docs: Move Jaeger index into bundle * Docs: Revise Loki docs * Docs: Move Loki index into bundle * Docs: Revise MS SQL docs * Docs: Move MS SQL index into bundle * Docs: Revise Prometheus docs * Docs: Move Prometheus index into bundle * Docs: Revise Tempo docs * Docs: Move Tempo index into bundle * Docs: Revise TestData DB docs * Docs: Move TestData DB index into bundle * Docs: Revise Zipkin docs * Docs: Move Zipkin index into bundle * Docs: Move other data sources' index pages into bundles * Docs: Revise frontmatter * Fixing hugo markdown errors * Docs: Add query editor and template var sections to overview doc * Docs: Remove CTAs across data source docs * Docs: Remove CTA * Docs: Remove CTA * Docs: Fix links, images, typos, and usage consistency. * Docs: Fix typos * Docs: Fix CI issues * Update docs/sources/datasources/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Update docs/sources/datasources/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Docs: Fix query editor links * Update docs/sources/panels-visualizations/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Update docs/sources/panels-visualizations/_index.md Co-authored-by: Torkel Ödegaard <torkel@grafana.com> * Docs: Rebundle child pages per writers' toolkit * Docs: Fix prettier for CI * Docs: Fix relrefs from outside data sources docs * Docs: Fix broken relrefs within datasources * Docs: Fix relrefs to data sources docs * Fixed some more refs Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
96 lines
3.9 KiB
Markdown
96 lines
3.9 KiB
Markdown
---
|
|
aliases:
|
|
- /docs/grafana/latest/data-sources/mssql/template-variables/
|
|
description: Using template variables with Microsoft SQL Server in Grafana
|
|
keywords:
|
|
- grafana
|
|
- MSSQL
|
|
- Microsoft
|
|
- SQL
|
|
- Azure SQL Database
|
|
- templates
|
|
- variables
|
|
- queries
|
|
menuTitle: Template variables
|
|
title: Microsoft SQL Server template variables
|
|
weight: 400
|
|
---
|
|
|
|
# Microsoft SQL Server template variables
|
|
|
|
Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
|
|
Grafana lists these variables in dropdown 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.
|
|
|
|
For an introduction to templating and template variables, refer to the [Templating]({{< relref "../../../dashboards/variables" >}}) and [Add and manage variables]({{< relref "../../../dashboards/variables/add-template-variables" >}}) documentation.
|
|
|
|
## Query variable
|
|
|
|
If you add a template variable of the type `Query`, you can write a MS SQL query that can
|
|
return things like measurement names, key names or key values that are shown as a dropdown select box.
|
|
|
|
For example, you can have a variable that contains all values for the `hostname` column in a table if you specify a query like this in the templating variable **Query** setting.
|
|
|
|
```sql
|
|
SELECT hostname FROM host
|
|
```
|
|
|
|
A query can return multiple columns and Grafana will automatically create a list from them. For example, the query below will return a list with values from `hostname` and `hostname2`.
|
|
|
|
```sql
|
|
SELECT [host].[hostname], [other_host].[hostname2] FROM host JOIN other_host ON [host].[city] = [other_host].[city]
|
|
```
|
|
|
|
Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allow you to have a friendly name as text and an id as the value. An example query with `hostname` as the text and `id` as the value:
|
|
|
|
```sql
|
|
SELECT hostname __text, id __value FROM host
|
|
```
|
|
|
|
You can also create nested variables. For example, if you had another variable named `region`. Then you could have
|
|
the hosts variable only show hosts from the current selected region with a query like this (if `region` is a multi-value variable, then use the `IN` comparison operator rather than `=` to match against multiple values):
|
|
|
|
```sql
|
|
SELECT hostname FROM host WHERE region IN ($region)
|
|
```
|
|
|
|
## Using variables in queries
|
|
|
|
> From Grafana 4.3.0 to 4.6.0, template variables are always quoted automatically so if it is a string value do not wrap them in quotes in where clauses.
|
|
>
|
|
> From Grafana 5.0.0, template variable values are only quoted when the template variable is a `multi-value`.
|
|
|
|
If the variable is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values.
|
|
|
|
There are two syntaxes:
|
|
|
|
`$<varname>` Example with a template variable named `hostname`:
|
|
|
|
```sql
|
|
SELECT
|
|
atimestamp time,
|
|
aint value
|
|
FROM table
|
|
WHERE $__timeFilter(atimestamp) and hostname in($hostname)
|
|
ORDER BY atimestamp
|
|
```
|
|
|
|
`[[varname]]` Example with a template variable named `hostname`:
|
|
|
|
```sql
|
|
SELECT
|
|
atimestamp as time,
|
|
aint as value
|
|
FROM table
|
|
WHERE $__timeFilter(atimestamp) and hostname in([[hostname]])
|
|
ORDER BY atimestamp
|
|
```
|
|
|
|
### Disabling Quoting for Multi-value Variables
|
|
|
|
Grafana automatically creates a quoted, comma-separated string for multi-value variables. For example: if `server01` and `server02` are selected then it will be formatted as: `'server01', 'server02'`. Do disable quoting, use the csv formatting option for variables:
|
|
|
|
`${servers:csv}`
|
|
|
|
Read more about variable formatting options in the [Variables]({{< relref "../../../dashboards/variables/variable-syntax#advanced-variable-format-options" >}}) documentation.
|