<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Microsoft SQL Server data source on Grafana Labs</title><link>https://grafana.com/docs/grafana/v12.4/datasources/mssql/</link><description>Recent content in Microsoft SQL Server data source on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/grafana/v12.4/datasources/mssql/index.xml" rel="self" type="application/rss+xml"/><item><title>Configure the Microsoft SQL Server data source</title><link>https://grafana.com/docs/grafana/v12.4/datasources/mssql/configure/</link><pubDate>Fri, 03 Apr 2026 19:43:06 +0000</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/mssql/configure/</guid><content><![CDATA[&lt;h1 id=&#34;configure-the-microsoft-sql-server-data-source&#34;&gt;Configure the Microsoft SQL Server data source&lt;/h1&gt;
&lt;p&gt;This document provides instructions for configuring the Microsoft SQL Server data source and explains available configuration options. For general information on adding and managing data sources, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/&#34;&gt;Grafana data sources&lt;/a&gt; and 
    &lt;a href=&#34;/docs/grafana/v12.4/administration/data-source-management/&#34;&gt;Data source management&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;Before configuring the Microsoft SQL Server data source, ensure you have the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Grafana permissions:&lt;/strong&gt; You must have the &lt;code&gt;Organization administrator&lt;/code&gt; role to configure data sources. Organization administrators can also &lt;a href=&#34;#provision-the-data-source&#34;&gt;configure the data source via YAML&lt;/a&gt; with the Grafana provisioning system.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A running SQL Server instance:&lt;/strong&gt; Microsoft SQL Server 2005 or newer, Azure SQL Database, or Azure SQL Managed Instance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Network access:&lt;/strong&gt; Grafana must be able to reach your SQL Server. The default port is &lt;code&gt;1433&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Authentication credentials:&lt;/strong&gt; Depending on your authentication method, you need one of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SQL Server login credentials (username and password).&lt;/li&gt;
&lt;li&gt;Windows/Kerberos credentials and configuration (not supported in Grafana Cloud).&lt;/li&gt;
&lt;li&gt;Azure Entra ID app registration or managed identity.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Security certificates:&lt;/strong&gt; If using encrypted connections, gather any necessary TLS/SSL certificates.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Grafana ships with a built-in Microsoft SQL Server data source plugin. No additional installation is required.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;



&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Grafana Cloud users:&lt;/strong&gt; If your SQL Server is in a private network, you can configure &lt;a href=&#34;/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/&#34;&gt;Private data source connect&lt;/a&gt; to establish connectivity.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h2 id=&#34;add-the-mssql-data-source&#34;&gt;Add the MSSQL data source&lt;/h2&gt;
&lt;p&gt;To add the MSSQL data source, complete the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Connections&lt;/strong&gt; in the left-side menu.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add new connection&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;Microsoft SQL Server&lt;/code&gt; in the search bar.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Microsoft SQL Server&lt;/strong&gt; under data source.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add new data source&lt;/strong&gt; in the upper right.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Grafana takes you to the &lt;strong&gt;Settings&lt;/strong&gt; tab, where you will set up your Microsoft SQL Server configuration.&lt;/p&gt;
&lt;h2 id=&#34;configure-the-data-source-in-the-ui&#34;&gt;Configure the data source in the UI&lt;/h2&gt;
&lt;p&gt;Following are configuration options for the Microsoft SQL Server data source.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-warning&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Warning&lt;/p&gt;&lt;p&gt;Kerberos is not supported in Grafana Cloud.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;strong&gt;Setting&lt;/strong&gt;&lt;/th&gt;
              &lt;th&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;The data source name. Sets the name you use to refer to the data source in panels and queries. Examples: &lt;code&gt;MSSQL-1&lt;/code&gt;, &lt;code&gt;MSSQL_Sales1&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Default&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Toggle to select as the default name in dashboard panels. When you go to a dashboard panel, this will be the default selected data source.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Connection:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Host&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Sets the IP address or hostname (and optional port) of your MSSQL instance. The default port is &lt;code&gt;0&lt;/code&gt;, which uses the driver&amp;rsquo;s default. &lt;br&gt; You can include additional connection properties (e.g., &lt;code&gt;ApplicationIntent&lt;/code&gt;) by separating them with semicolons (&lt;code&gt;;&lt;/code&gt;).&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Database&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Sets the name of the MSSQL database to connect to.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;TLS/SSL Auth:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Encrypt - Determines whether or to which extent a secure SSL TCP/IP connection will be negotiated with the server.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Encrypt Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Disable&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Data sent between the client and server is &lt;strong&gt;not encrypted&lt;/strong&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;False&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;The default setting. Only the login packet is encrypted; &lt;strong&gt;all other data is sent unencrypted&lt;/strong&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;True&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;&lt;strong&gt;All data&lt;/strong&gt; sent between the client and server is &lt;strong&gt;encrypted&lt;/strong&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;

&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;If you&amp;rsquo;re using an older version of Microsoft SQL Server like 2008 and 2008R2, you may need to disable encryption to be able to connect.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Authentication:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Authentication Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
              &lt;th&gt;Credentials / Fields&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;SQL Server Authentication&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Default method to connect to MSSQL. Use a SQL Server or Windows login in &lt;code&gt;DOMAIN\User&lt;/code&gt; format.&lt;/td&gt;
              &lt;td&gt;- &lt;strong&gt;Username&lt;/strong&gt;: SQL Server username&lt;br&gt;- &lt;strong&gt;Password&lt;/strong&gt;: SQL Server password&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Windows Authentication&lt;/strong&gt;&lt;br&gt;(Integrated Security)&lt;/td&gt;
              &lt;td&gt;Uses the logged-in Windows user&amp;rsquo;s credentials via single sign-on. Available only when SQL Server allows Windows Authentication.&lt;/td&gt;
              &lt;td&gt;No input required; uses the logged-in Windows user&amp;rsquo;s credentials&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Windows AD&lt;/strong&gt;&lt;br&gt;(Username/Password)&lt;/td&gt;
              &lt;td&gt;Authenticates a domain user with their Active Directory username and password.&lt;/td&gt;
              &lt;td&gt;- &lt;strong&gt;Username&lt;/strong&gt;: &lt;code&gt;user@example.com&lt;/code&gt;&lt;br&gt;- &lt;strong&gt;Password&lt;/strong&gt;: Active Directory password&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Windows AD&lt;/strong&gt;&lt;br&gt;(Keytab)&lt;/td&gt;
              &lt;td&gt;Authenticates a domain user using a keytab file.&lt;/td&gt;
              &lt;td&gt;- &lt;strong&gt;Username&lt;/strong&gt;: &lt;code&gt;user@example.com&lt;/code&gt;&lt;br&gt;- &lt;strong&gt;Keytab file path&lt;/strong&gt;: Path to your keytab file&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Windows AD&lt;/strong&gt;&lt;br&gt;(Credential Cache)&lt;/td&gt;
              &lt;td&gt;Uses a Kerberos credential cache already loaded in memory (e.g., from a prior &lt;code&gt;kinit&lt;/code&gt; command). No file needed.&lt;/td&gt;
              &lt;td&gt;- &lt;strong&gt;Credential cache path&lt;/strong&gt;: Path to in-memory credential (e.g., &lt;code&gt;/tmp/krb5cc_1000&lt;/code&gt;)&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Windows AD&lt;/strong&gt;&lt;br&gt;(Credential Cache File)&lt;/td&gt;
              &lt;td&gt;Authenticates a domain user using a credential cache file (&lt;code&gt;.ccache&lt;/code&gt;).&lt;/td&gt;
              &lt;td&gt;- &lt;strong&gt;Username&lt;/strong&gt;: &lt;code&gt;user@example.com&lt;/code&gt;&lt;br&gt;- &lt;strong&gt;Credential cache file path&lt;/strong&gt;: e.g., &lt;code&gt;/home/grot/cache.json&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Azure Entra ID (formerly Azure AD) Authentication&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Authenticates the data source using Azure authentication methods.&lt;/td&gt;
              &lt;td&gt;Details on the supported authentication methods and how to configure them can be found in the &lt;a href=&#34;./index.md#azure-entra-id-formerly-azure-ad-authentication&#34;&gt;Azure authentication section&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Additional settings:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Additional settings are optional settings you configure for more control over your data source. This includes connection limits, connection timeout, group-by time interval, and Secure Socks Proxy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Connection limits&lt;/strong&gt;:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Max open&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;The maximum number of open connections to the database. If set to &lt;code&gt;0&lt;/code&gt;, there is no limit. If &lt;code&gt;max open&lt;/code&gt; is greater than &lt;code&gt;0&lt;/code&gt; and less than &lt;code&gt;max idle&lt;/code&gt;, &lt;code&gt;max idle&lt;/code&gt; is adjusted to match.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Auto max idle&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;When enabled, automatically sets &lt;code&gt;max idle&lt;/code&gt; to match &lt;code&gt;max open&lt;/code&gt;. If &lt;code&gt;max open&lt;/code&gt; isn’t set, it defaults to &lt;code&gt;100&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Max idle&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;The maximum number of idle connections in the pool. If &lt;code&gt;max open&lt;/code&gt; is set and is lower than &lt;code&gt;max idle&lt;/code&gt;, then &lt;code&gt;max idle&lt;/code&gt; is reduced to match. If set to &lt;code&gt;0&lt;/code&gt;, no idle connections are retained.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Max lifetime&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;The maximum time (in seconds) a connection can be reused before being closed and replaced. If set to &lt;code&gt;0&lt;/code&gt;, connections are reused indefinitely.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Connection details:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;strong&gt;Setting&lt;/strong&gt;&lt;/th&gt;
              &lt;th&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Min time interval&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Specifies the lower bound for the auto-generated &lt;code&gt;GROUP BY&lt;/code&gt; time interval. Grafana recommends matching this value to your data&amp;rsquo;s write frequency—for example, &lt;code&gt;1m&lt;/code&gt; if data is written every minute. Refer to &lt;a href=&#34;#min-time-interval&#34;&gt;Min time interval&lt;/a&gt; for details.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Connection timeout&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Specifies the maximum number of seconds to wait when attempting to connect to the database before timing out. A value of &lt;code&gt;0&lt;/code&gt; (the default) disables the timeout.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Windows ADS Advanced Settings&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
              &lt;th&gt;Default&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;UDP Preference Limit&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Defines the maximum packet size (in bytes) that Kerberos libraries will attempt to send over UDP before retrying with TCP. A value of &lt;code&gt;1&lt;/code&gt; forces all communication to use TCP.&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;1&lt;/code&gt; (always use TCP)&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;DNS Lookup KDC&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Controls whether DNS &lt;code&gt;SRV&lt;/code&gt; records are used to locate &lt;a href=&#34;https://web.mit.edu/kerberos/krb5-latest/doc/admin/realm_config.html#key-distribution-centers&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Key Distribution Centers (KDCs)&lt;/a&gt; and other servers for the realm.&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;krb5 config file path&lt;/strong&gt;&lt;/td&gt;
              &lt;td&gt;Specifies the path to the Kerberos configuration file used by the &lt;a href=&#34;https://web.mit.edu/kerberos/krb5-1.12/doc/admin/conf_files/krb5_conf.html&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;MIT krb5 package&lt;/a&gt;.&lt;/td&gt;
              &lt;td&gt;&lt;code&gt;/etc/krb5.conf&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Private data source connect&lt;/strong&gt; - &lt;em&gt;Only for Grafana Cloud users.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Private data source connect, or PDC, allows you to establish a private, secured connection between a Grafana Cloud instance, or stack, and data sources secured within a private network. Click the drop-down to locate the URL for PDC. For more information regarding Grafana PDC refer to &lt;a href=&#34;/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/&#34;&gt;Private data source connect (PDC)&lt;/a&gt; and &lt;a href=&#34;/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/configure-pdc/#configure-grafana-private-data-source-connect-pdc&#34;&gt;Configure Grafana private data source connect (PDC)&lt;/a&gt; for instructions on setting up a PDC connection.&lt;/p&gt;
&lt;p&gt;Click &lt;strong&gt;Manage private data source connect&lt;/strong&gt; to open your PDC connection page and view your configuration details.&lt;/p&gt;
&lt;p&gt;After configuring your MSSQL data source options, click &lt;strong&gt;Save &amp;amp; test&lt;/strong&gt; at the bottom to test the connection. You should see a confirmation dialog box that says:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Database Connection OK&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&#34;azure-entra-id-formerly-azure-ad-authentication&#34;&gt;Azure Entra ID (formerly Azure AD) Authentication&lt;/h3&gt;
&lt;p&gt;The following Azure authentication methods are supported:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Current User authentication&lt;/li&gt;
&lt;li&gt;App Registration&lt;/li&gt;
&lt;li&gt;Managed Identity&lt;/li&gt;
&lt;li&gt;Azure Entra Password&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Azure SQL Server that you are connecting to should support Azure Entra authentication to support adding the App Registration as a user in the database. For configuration details, refer to the &lt;a href=&#34;https://learn.microsoft.com/en-us/azure/azure-sql/database/authentication-aad-configure?view=azuresql&amp;amp;tabs=azure-portal&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Azure SQL documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&#34;current-user-authentication&#34;&gt;Current User authentication&lt;/h4&gt;
&lt;p&gt;This is the recommended authentication mechanism when working with SQL Server instances that are hosted in Azure. It allows users to be authenticated to and query the database using their own credentials rather than long-lived credentials.&lt;/p&gt;
&lt;p&gt;This authentication method requires your Grafana instance to be configured with Azure Entra ID (formerly Active Directory) authentication for login. With Azure Entra ID login, this method can be used to forward the currently logged in user’s credentials to the data source. The users credentials will then be used when requesting data from the data source. For details on how to configure your Grafana instance using Azure Entra refer to the 
    &lt;a href=&#34;/docs/grafana/v12.4/setup-grafana/configure-access/configure-authentication/azuread/&#34;&gt;documentation&lt;/a&gt;.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Additional configuration is required to ensure that the App Registration used to login a user via Azure provides an access token with the permissions required by the data source.&lt;/p&gt;
&lt;p&gt;The App Registration must be configured to issue both &lt;strong&gt;Access Tokens&lt;/strong&gt; and &lt;strong&gt;ID Tokens&lt;/strong&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In the Azure Portal, open the App Registration that requires configuration.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Authentication&lt;/strong&gt; in the side menu.&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Implicit grant and hybrid flows&lt;/strong&gt; check both the &lt;strong&gt;Access tokens&lt;/strong&gt; and &lt;strong&gt;ID tokens&lt;/strong&gt; boxes.&lt;/li&gt;
&lt;li&gt;Save the changes to ensure the App Registration is updated.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The App Registration must also be configured with additional &lt;strong&gt;API Permissions&lt;/strong&gt; to provide authenticated users with access to the APIs utilised by the data source.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In the Azure Portal, open the App Registration that requires configuration.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;API Permissions&lt;/strong&gt; in the side menu.&lt;/li&gt;
&lt;li&gt;Ensure the &lt;code&gt;openid&lt;/code&gt;, &lt;code&gt;profile&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, and &lt;code&gt;offline_access&lt;/code&gt; permissions are present under the &lt;strong&gt;Microsoft Graph&lt;/strong&gt; section. If not, they must be added.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Add a permission&lt;/strong&gt; and choose the following permissions. They must be added individually. Refer to the &lt;a href=&#34;https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-configure-app-access-web-apis&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Azure documentation&lt;/a&gt; for more information.
&lt;ul&gt;
&lt;li&gt;Select &lt;strong&gt;APIs my organization uses&lt;/strong&gt; &amp;gt; Search for &lt;strong&gt;Azure SQL&lt;/strong&gt; and select it &amp;gt; &lt;strong&gt;Delegated permissions&lt;/strong&gt; &amp;gt; &lt;code&gt;user_impersonation&lt;/code&gt; &amp;gt; &lt;strong&gt;Add permissions&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After all permissions have been added, the Azure authentication section in Grafana must be updated. The &lt;code&gt;scopes&lt;/code&gt; section must be updated to include the &lt;code&gt;.default&lt;/code&gt; scope to ensure that a token with access to all APIs declared on the App Registration is requested by Grafana. After updated the scopes value should equal: &lt;code&gt;.default openid email profile&lt;/code&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;This method of authentication doesn&amp;rsquo;t inherently support all backend functionality as a user&amp;rsquo;s credentials won&amp;rsquo;t be in scope. Affected functionality includes alerting, reporting, and recorded queries. Also, note that query and resource caching is disabled by default for data sources using current user authentication.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To enable current user authentication for Grafana:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Set the &lt;code&gt;user_identity_enabled&lt;/code&gt; flag in the &lt;code&gt;[azure]&lt;/code&gt; section of the 
    &lt;a href=&#34;/docs/grafana/v12.4/setup-grafana/configure-grafana/#azure&#34;&gt;Grafana server configuration&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;ini&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-ini&#34;&gt;[azure]
user_identity_enabled = true&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the SQL Server data source configuration, set &lt;strong&gt;Authentication&lt;/strong&gt; to &lt;strong&gt;Azure AD Authentication&lt;/strong&gt; and the Azure Authentication type to &lt;strong&gt;Current User&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;app-registration&#34;&gt;App Registration&lt;/h3&gt;
&lt;p&gt;You must create an app registration and service principal in Azure Entra to authenticate the data source.
For configuration details, refer to the &lt;a href=&#34;https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal#get-tenant-and-app-id-values-for-signing-in&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Azure documentation for service principals&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After the app registration has been created, make note of the tenant ID, client ID, and client secret. Take the following steps to add the app registration as a SQL user:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Connect to your Azure SQL database as a user with administrative permissions (the user used here must have the ability to read your Azure Entra directory e.g. by possessing the &lt;code&gt;Directory Readers&lt;/code&gt; role).&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;CREATE USER [$IDENTITY_NAME] FROM EXTERNAL PROVIDER;&lt;/code&gt;, substituting &lt;code&gt;IDENTITY_NAME&lt;/code&gt; with the app registration name.&lt;/li&gt;
&lt;li&gt;Grant the created user the appropriate level of permissions for your use-case. It is recommended that users configured for data sources only have reader permissions.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After the appropriate permissions have been granted, configure the SQL Server data source to use the app registration:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In the SQL Server data source configuration, set &lt;strong&gt;Authentication&lt;/strong&gt; to &lt;strong&gt;Azure AD Authentication&lt;/strong&gt; and the Azure Authentication type to &lt;strong&gt;App Registration&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Set the &lt;strong&gt;Azure Cloud&lt;/strong&gt; value to the correct value. If you are using the Azure public cloud this will be &lt;strong&gt;Azure&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Set the &lt;strong&gt;Directory (tenant) ID&lt;/strong&gt;, &lt;strong&gt;Application (client) ID&lt;/strong&gt;, and &lt;strong&gt;Client Secret&lt;/strong&gt; values to those for your app registration.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;managed-identity&#34;&gt;Managed Identity&lt;/h3&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Managed Identity is available only in &lt;a href=&#34;https://azure.microsoft.com/en-us/products/managed-grafana&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Azure Managed Grafana&lt;/a&gt; or Grafana OSS/Enterprise when deployed in Azure. It is not available in Grafana Cloud.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;You can use managed identity to configure SQL Server in Grafana if you host Grafana in Azure (such as an App Service or with Azure Virtual Machines) and have managed identity enabled on your VM.
This lets you securely authenticate data sources without manually configuring credentials via Azure AD App Registrations.
For details on Azure managed identities, refer to the &lt;a href=&#34;https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Azure documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To enable managed identity for Grafana:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Set the &lt;code&gt;managed_identity_enabled&lt;/code&gt; flag in the &lt;code&gt;[azure]&lt;/code&gt; section of the 
    &lt;a href=&#34;/docs/grafana/v12.4/setup-grafana/configure-grafana/#azure&#34;&gt;Grafana server configuration&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;ini&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-ini&#34;&gt;[azure]
managed_identity_enabled = true&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the SQL Server data source configuration, set &lt;strong&gt;Authentication&lt;/strong&gt; to &lt;strong&gt;Azure AD Authentication&lt;/strong&gt; and the Azure Authentication type to &lt;strong&gt;Managed Identity&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This hides the directory ID, application ID, and client secret fields, and the data source uses managed identity to authenticate to SQL Server.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can set the &lt;code&gt;managed_identity_client_id&lt;/code&gt; field in the &lt;code&gt;[azure]&lt;/code&gt; section of the 
    &lt;a href=&#34;/docs/grafana/v12.4/setup-grafana/configure-grafana/#azure&#34;&gt;Grafana server configuration&lt;/a&gt; to allow a user-assigned managed identity to be used instead of the default system-assigned identity.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ensure that the managed identity used is added to your Azure SQL instance as a user.&lt;/p&gt;
&lt;h3 id=&#34;azure-entra-password&#34;&gt;Azure Entra Password&lt;/h3&gt;


&lt;div class=&#34;admonition admonition-warning&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Warning&lt;/p&gt;&lt;p&gt;Azure Entra Password is not a recommended authentication mechanism as it requires configuration using a single users password. Consider an alternative authentication method such as current user authentication or app registration.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;You can connect to an Azure SQL database using the username and password of a user that has permissions in the desired database. This also requires an app registration to be configured with access to the database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To enable Azure Entra password for Grafana:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Set the &lt;code&gt;azure_entra_password_credentials_enabled&lt;/code&gt; flag in the &lt;code&gt;[azure]&lt;/code&gt; section of the 
    &lt;a href=&#34;/docs/grafana/v12.4/setup-grafana/configure-grafana/#azure&#34;&gt;Grafana server configuration&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;ini&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-ini&#34;&gt;[azure]
azure_entra_password_credentials_enabled = true&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the SQL Server data source configuration, set &lt;strong&gt;Authentication&lt;/strong&gt; to &lt;strong&gt;Azure AD Authentication&lt;/strong&gt; and the Azure Authentication type to &lt;strong&gt;Azure Entra Password&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Set the &lt;strong&gt;User ID&lt;/strong&gt; value to the username of the user in the Azure SQL database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Set the &lt;strong&gt;Application Client ID&lt;/strong&gt; to the client ID of the app registration that has been added to the Azure SQL database&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Set the &lt;strong&gt;Password&lt;/strong&gt; value to the password of the user in the Azure SQL database.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;min-time-interval&#34;&gt;Min time interval&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;Min time interval&lt;/strong&gt; setting defines a lower limit for the 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/#__interval&#34;&gt;&lt;code&gt;$__interval&lt;/code&gt;&lt;/a&gt; and [&lt;code&gt;$__interval_ms&lt;/code&gt;][add-template-variables-interval_ms] variables.&lt;/p&gt;
&lt;p&gt;This value &lt;em&gt;must&lt;/em&gt; be formatted as a number followed by a valid time identifier:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Identifier&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;y&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;year&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;M&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;month&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;w&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;week&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;d&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;day&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;h&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;hour&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;m&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;minute&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;s&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;second&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;ms&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;millisecond&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;Grafana recommends setting this value to match your Microsoft SQL Server write frequency.
For example, use &lt;code&gt;1m&lt;/code&gt; if Microsoft SQL Server writes data every minute.&lt;/p&gt;
&lt;p&gt;You can also override this setting in a dashboard panel under its data source options.&lt;/p&gt;
&lt;h3 id=&#34;database-user-permissions&#34;&gt;Database user permissions&lt;/h3&gt;
&lt;p&gt;When adding a data source, ensure the database user you specify has only SELECT permissions on the relevant database and tables. Grafana does not validate the safety of queries, which means they can include potentially harmful SQL statements, such as &lt;code&gt;USE otherdb&lt;/code&gt;; or &lt;code&gt;DROP TABLE user;&lt;/code&gt;, which could get executed. To minimize this risk, Grafana strongly recommends creating a dedicated MySQL user with restricted permissions.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;CREATE USER grafanareader WITH PASSWORD &amp;#39;password&amp;#39;
GRANT SELECT ON dbo.YourTable3 TO grafanareader&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Also, ensure that the user doesn&amp;rsquo;t have any unwanted privileges from the public role.&lt;/p&gt;
&lt;h3 id=&#34;diagnose-connection-issues&#34;&gt;Diagnose connection issues&lt;/h3&gt;
&lt;p&gt;If you use older versions of Microsoft SQL Server, such as 2008 and 2008R2, you might need to disable encryption before you can connect the data source.&lt;/p&gt;
&lt;p&gt;Grafana recommends that you use the latest available service pack for optimal compatibility.&lt;/p&gt;
&lt;h3 id=&#34;provision-the-data-source&#34;&gt;Provision the data source&lt;/h3&gt;
&lt;p&gt;You can define and configure the data source in YAML files as part of the Grafana provisioning system. For more information about provisioning, and for available configuration options, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/administration/provisioning/&#34;&gt;Provision Grafana&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&#34;provisioning-example&#34;&gt;Provisioning example&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;apiVersion: 1

datasources:
  - name: MSSQL
    type: mssql
    url: localhost:1433
    user: grafana
    jsonData:
      database: grafana
      maxOpenConns: 100
      maxIdleConns: 100
      maxIdleConnsAuto: true
      connMaxLifetime: 14400
      connectionTimeout: 0
      encrypt: &amp;#39;false&amp;#39;
    secureJsonData:
      password: &amp;#39;Password!&amp;#39;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;configure-with-terraform&#34;&gt;Configure with Terraform&lt;/h3&gt;
&lt;p&gt;You can configure the Microsoft SQL Server data source using &lt;a href=&#34;https://www.terraform.io/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Terraform&lt;/a&gt; with the &lt;a href=&#34;https://registry.terraform.io/providers/grafana/grafana/latest/docs&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana Terraform provider&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For more information about provisioning resources with Terraform, refer to the &lt;a href=&#34;/docs/grafana-cloud/developer-resources/infrastructure-as-code/terraform/&#34;&gt;Grafana as code using Terraform&lt;/a&gt; documentation.&lt;/p&gt;
&lt;h4 id=&#34;terraform-example&#34;&gt;Terraform example&lt;/h4&gt;
&lt;p&gt;The following example creates a basic Microsoft SQL Server data source:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;hcl&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-hcl&#34;&gt;resource &amp;#34;grafana_data_source&amp;#34; &amp;#34;mssql&amp;#34; {
  name = &amp;#34;MSSQL&amp;#34;
  type = &amp;#34;mssql&amp;#34;
  url  = &amp;#34;localhost:1433&amp;#34;
  user = &amp;#34;grafana&amp;#34;

  json_data_encoded = jsonencode({
    database           = &amp;#34;grafana&amp;#34;
    maxOpenConns       = 100
    maxIdleConns       = 100
    maxIdleConnsAuto   = true
    connMaxLifetime    = 14400
    connectionTimeout  = 0
    encrypt            = &amp;#34;false&amp;#34;
  })

  secure_json_data_encoded = jsonencode({
    password = &amp;#34;Password!&amp;#34;
  })
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;For all available configuration options, refer to the &lt;a href=&#34;https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/data_source&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana provider data source resource documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;next-steps&#34;&gt;Next steps&lt;/h2&gt;
&lt;p&gt;After configuring your Microsoft SQL Server data source, you can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/mssql/query-editor/&#34;&gt;Write queries&lt;/a&gt; using the query editor to explore and visualize your data&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/mssql/template-variables/&#34;&gt;Create template variables&lt;/a&gt; to build dynamic, reusable dashboards&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/build-dashboards/annotate-visualizations/&#34;&gt;Add annotations&lt;/a&gt; to overlay SQL Server events on your graphs&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/alerting/&#34;&gt;Set up alerting&lt;/a&gt; to create alert rules based on your SQL Server data&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/mssql/troubleshooting/&#34;&gt;Troubleshoot issues&lt;/a&gt; if you encounter problems with your data source&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="configure-the-microsoft-sql-server-data-source">Configure the Microsoft SQL Server data source&lt;/h1>
&lt;p>This document provides instructions for configuring the Microsoft SQL Server data source and explains available configuration options. For general information on adding and managing data sources, refer to
&lt;a href="/docs/grafana/v12.4/datasources/">Grafana data sources&lt;/a> and
&lt;a href="/docs/grafana/v12.4/administration/data-source-management/">Data source management&lt;/a>.&lt;/p></description></item><item><title>Microsoft SQL Server query editor</title><link>https://grafana.com/docs/grafana/v12.4/datasources/mssql/query-editor/</link><pubDate>Fri, 03 Apr 2026 19:43:06 +0000</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/mssql/query-editor/</guid><content><![CDATA[&lt;h1 id=&#34;microsoft-sql-server-query-editor&#34;&gt;Microsoft SQL Server query editor&lt;/h1&gt;
&lt;p&gt;Grafana provides a query editor for the Microsoft SQL Server data source, which is located on the 
    &lt;a href=&#34;/docs/grafana/v12.4/explore/&#34;&gt;Explore page&lt;/a&gt;. You can also access the MSSQL query editor from a dashboard panel. Click the menu in the upper right of the panel and select &lt;strong&gt;Edit&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This topic explains querying specific to the MSSQL data source.
For general documentation on querying data sources in Grafana, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/query-transform-data/&#34;&gt;Query and transform data&lt;/a&gt;. For options and functions common to all query editors, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/query-transform-data/&#34;&gt;Query editors&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For more information on writing Transact-SQL statements, refer to &lt;a href=&#34;https://learn.microsoft.com/en-us/sql/t-sql/tutorial-writing-transact-sql-statements?view=sql-server-ver17&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Write Transact-SQL statements&lt;/a&gt; and &lt;a href=&#34;https://learn.microsoft.com/en-us/sql/t-sql/language-reference?view=sql-server-ver17&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Transact-SQL reference&lt;/a&gt; in the Microsoft SQL Server documentation.&lt;/p&gt;
&lt;p&gt;The Microsoft SQL Server query editor has two modes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#builder-mode&#34;&gt;Builder mode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#code-mode&#34;&gt;Code mode&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To switch between the editor modes, select the corresponding &lt;strong&gt;Builder&lt;/strong&gt; and &lt;strong&gt;Code&lt;/strong&gt; tabs in the upper right.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;/media/mssql/mssql-query-editor-v12.png&#34;
  alt=&#34;MSSQL query builder&#34; width=&#34;1243&#34;
     height=&#34;286&#34;/&gt;&lt;/p&gt;


&lt;div class=&#34;admonition admonition-warning&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Warning&lt;/p&gt;&lt;p&gt;When switching from &lt;strong&gt;Code&lt;/strong&gt; mode to &lt;strong&gt;Builder&lt;/strong&gt; mode, any changes made to your SQL query aren&amp;rsquo;t saved and will not be shown in the builder interface. You can choose to copy your code to the clipboard or discard the changes.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;To run a query, select &lt;strong&gt;Run query&lt;/strong&gt; in the upper right of the editor.&lt;/p&gt;
&lt;p&gt;In addition to writing queries, the query editor also allows you to create and use:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#macros&#34;&gt;Macros&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#apply-annotations&#34;&gt;Annotations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#use-stored-procedures&#34;&gt;Stored procedures&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;builder-mode&#34;&gt;Builder mode&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Builder mode&lt;/strong&gt; allows you to build queries using a visual interface. This mode is great for users who prefer a guided query experience or are just getting started with SQL.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow&#34;
    style=&#34;max-width: 1098px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/mssql/mssql-builder-mode-v12.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/mssql/mssql-builder-mode-v12.png&#34;data-srcset=&#34;/media/docs/mssql/mssql-builder-mode-v12.png?w=320 320w, /media/docs/mssql/mssql-builder-mode-v12.png?w=550 550w, /media/docs/mssql/mssql-builder-mode-v12.png?w=750 750w, /media/docs/mssql/mssql-builder-mode-v12.png?w=900 900w, /media/docs/mssql/mssql-builder-mode-v12.png?w=1040 1040w, /media/docs/mssql/mssql-builder-mode-v12.png?w=1240 1240w, /media/docs/mssql/mssql-builder-mode-v12.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;MSSQL builder mode&amp;gt;&#34;width=&#34;1098&#34;height=&#34;567&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/mssql/mssql-builder-mode-v12.png&#34;
            alt=&#34;MSSQL builder mode&amp;gt;&#34;width=&#34;1098&#34;height=&#34;567&#34;class=&#34;docs-image--no-shadow&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;The following components will help you build a T-SQL query:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Format&lt;/strong&gt; - Select a format response from the drop-down for the MSSQL query. The default is &lt;strong&gt;Table&lt;/strong&gt;. Refer to &lt;a href=&#34;#table-queries&#34;&gt;Table queries&lt;/a&gt; and &lt;a href=&#34;#time-series-queries&#34;&gt;Time series queries&lt;/a&gt; for more information and examples. If you select the &lt;strong&gt;Time series&lt;/strong&gt; format option, you must include a &lt;code&gt;time&lt;/code&gt; column.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dataset&lt;/strong&gt; - Select a database to query from the drop-down. Grafana automatically populates the drop-down with all databases the user has access to. If a default database is configured in the Data Source Configuration page or via a provisioning file, users will be limited to querying only that predefined database.&lt;/p&gt;
&lt;p&gt;Note that &lt;code&gt;tempdb&lt;/code&gt;, &lt;code&gt;model&lt;/code&gt;, &lt;code&gt;msdb&lt;/code&gt;, and &lt;code&gt;master&lt;/code&gt; system databases are not included in the query editor drop-down.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Table&lt;/strong&gt; - Select a table from the drop-down. After selecting a database, the next drop-down displays all available tables in that database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data operations&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt;. Select an aggregation or a macro from the drop-down. You can add multiple data operations by clicking the &lt;strong&gt;&#43; sign&lt;/strong&gt;. Click the &lt;strong&gt;garbage can icon&lt;/strong&gt; to remove data operations.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Column&lt;/strong&gt; - Select a column on which to run the aggregation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interval&lt;/strong&gt; - Select an interval from the drop-down. You&amp;rsquo;ll see this option when you choose a &lt;code&gt;time group&lt;/code&gt; macro from the drop-down.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fill&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt;. Add a &lt;code&gt;FILL&lt;/code&gt; method to populate missing time intervals with default values (such as NULL, 0, or a specified value) when no data exists for those intervals. This ensures continuity in the time series, avoiding gaps in visualizations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Alias&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt;. Add an alias from the drop-down. You can also add your own alias by typing it in the box and clicking &lt;strong&gt;Enter&lt;/strong&gt;. Remove an alias by clicking the &lt;strong&gt;X&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Filter&lt;/strong&gt; - Toggle to add filters.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Filter by column value&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt;. If you toggle &lt;strong&gt;Filter&lt;/strong&gt; you can add a column to filter by from the drop-down. To filter by additional columns, click the &lt;strong&gt;&#43; sign&lt;/strong&gt; to the right of the condition drop-down. You can choose a variety of operators from the drop-down next to the condition. When multiple filters are added, use the &lt;code&gt;AND&lt;/code&gt; or &lt;code&gt;OR&lt;/code&gt; operators to define how conditions are evaluated. &lt;code&gt;AND&lt;/code&gt; requires all conditions to be true, while &lt;code&gt;OR&lt;/code&gt; requires any condition to be true. Use the second drop-down to select the filter value. To remove a filter, click the &lt;strong&gt;X icon&lt;/strong&gt; next to it. If you select a &lt;code&gt;date-type&lt;/code&gt; column, you can use macros from the operator list and choose &lt;code&gt;timeFilter&lt;/code&gt; to insert the &lt;code&gt;$\_\_timeFilter&lt;/code&gt; macro into your query with the selected date column.&lt;/p&gt;
&lt;p&gt;After selecting a date type column, you can choose Macros from the operators list and select timeFilter which will add the &lt;code&gt;$\_\_timeFilter&lt;/code&gt; macro to the query with the selected date column. Refer to &lt;a href=&#34;#macros&#34;&gt;Macros&lt;/a&gt; for more information.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Group&lt;/strong&gt; - Toggle to add a &lt;code&gt;GROUP BY&lt;/code&gt; column.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Group by column&lt;/strong&gt; - Select a column to filter by from the drop-down. Click the &lt;strong&gt;&#43;sign&lt;/strong&gt; to filter by multiple columns. Click the &lt;strong&gt;X&lt;/strong&gt; to remove a filter.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Order&lt;/strong&gt; - Toggle to add an &lt;code&gt;ORDER BY&lt;/code&gt; statement.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Order by&lt;/strong&gt; - Select a column to order by from the drop-down. Select ascending (&lt;code&gt;ASC&lt;/code&gt;) or descending (&lt;code&gt;DESC&lt;/code&gt;) order.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Limit&lt;/strong&gt; - You can add an optional limit on the number of retrieved results. Default is 50.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Preview&lt;/strong&gt; - Toggle for a preview of the SQL query generated by the query builder. Preview is toggled on by default.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For additional detail about using formats, refer to &lt;a href=&#34;#table-queries&#34;&gt;Table queries&lt;/a&gt; and &lt;a href=&#34;#time-series-queries&#34;&gt;Time series queries&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;code-mode&#34;&gt;Code mode&lt;/h2&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow&#34;
    style=&#34;max-width: 963px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/static/img/docs/v92/sql_code_editor.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/static/img/docs/v92/sql_code_editor.png&#34;data-srcset=&#34;/static/img/docs/v92/sql_code_editor.png?w=320 320w, /static/img/docs/v92/sql_code_editor.png?w=550 550w, /static/img/docs/v92/sql_code_editor.png?w=750 750w, /static/img/docs/v92/sql_code_editor.png?w=900 900w, /static/img/docs/v92/sql_code_editor.png?w=1040 1040w, /static/img/docs/v92/sql_code_editor.png?w=1240 1240w, /static/img/docs/v92/sql_code_editor.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;&#34;width=&#34;963&#34;height=&#34;326&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/static/img/docs/v92/sql_code_editor.png&#34;
            alt=&#34;&#34;width=&#34;963&#34;height=&#34;326&#34;class=&#34;docs-image--no-shadow&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Code mode&lt;/strong&gt; lets you build complex queries using a text editor with helpful features like autocompletion and syntax highlighting.&lt;/p&gt;
&lt;p&gt;This mode is ideal for advanced users who need full control over the SQL query or want to use features not available in visual query mode. It’s especially useful for writing subqueries, using macros, or applying advanced filtering and formatting. You can switch back to visual mode, but note that some custom queries may not be fully compatible.&lt;/p&gt;
&lt;h3 id=&#34;code-mode-toolbar-features&#34;&gt;Code mode toolbar features&lt;/h3&gt;
&lt;p&gt;Code mode has several features in a toolbar located in the editor&amp;rsquo;s lower-right corner.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;To reformat the query, click the brackets button (&lt;code&gt;{}&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;To expand the code editor, click the chevron button pointing downward.&lt;/li&gt;
&lt;li&gt;To run the query, click the &lt;strong&gt;Run query&lt;/strong&gt; button or use the keyboard shortcut &lt;strong&gt;&lt;key&gt;Ctrl&lt;/key&gt;/&lt;key&gt;Cmd&lt;/key&gt; &#43; &lt;key&gt;Enter&lt;/key&gt;/&lt;key&gt;Return&lt;/key&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;use-autocompletion&#34;&gt;Use autocompletion&lt;/h3&gt;
&lt;p&gt;Code mode&amp;rsquo;s autocompletion feature works automatically while typing.
To manually trigger autocompletion, use the keyboard shortcut &lt;key&gt;Ctrl&lt;/key&gt;/&lt;key&gt;Cmd&lt;/key&gt; &#43; &lt;key&gt;Space&lt;/key&gt;.&lt;/p&gt;
&lt;p&gt;Code mode supports autocompletion of tables, columns, SQL keywords, standard SQL functions, Grafana template variables, and Grafana macros.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;You can&amp;rsquo;t autocomplete columns until you&amp;rsquo;ve specified a table.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h2 id=&#34;macros&#34;&gt;Macros&lt;/h2&gt;
&lt;p&gt;To simplify syntax and to allow for dynamic components, such as date range filters, you can add macros to your query.&lt;/p&gt;
&lt;p&gt;Use macros in the &lt;code&gt;SELECT&lt;/code&gt; clause to simplify the creation of time series queries.
From the &lt;strong&gt;Data operations&lt;/strong&gt; drop-down, choose a macro such as &lt;code&gt;$\_\_timeGroup&lt;/code&gt; or &lt;code&gt;$\_\_timeGroupAlias&lt;/code&gt;. Then, select a time column from the &lt;strong&gt;Column&lt;/strong&gt; drop-down and a time interval from the &lt;strong&gt;Interval&lt;/strong&gt; drop-down. This generates a time-series query based on your selected time grouping.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-warning&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Warning&lt;/p&gt;&lt;p&gt;Time macros (&lt;code&gt;$__time&lt;/code&gt;, &lt;code&gt;$__timeFilter&lt;/code&gt;, etc.) don&amp;rsquo;t support time zone parameters in Microsoft SQL Server and always expand to UTC values. If your timestamps aren&amp;rsquo;t stored in UTC (common with &lt;code&gt;datetime&lt;/code&gt;/&lt;code&gt;datetime2&lt;/code&gt; types), convert them to UTC in your SQL query using &lt;code&gt;AT TIME ZONE … AT TIME ZONE &#39;UTC&#39;&lt;/code&gt; rather than passing a time zone argument to a macro.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;strong&gt;Macro&lt;/strong&gt;&lt;/th&gt;
              &lt;th&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__time(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Renames the specified column to &lt;code&gt;_time&lt;/code&gt;. &lt;br/&gt;Example: &lt;code&gt;dateColumn AS time&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeEpoch(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Converts a &lt;code&gt;DATETIME&lt;/code&gt; column to a Unix timestamp and renames it to &lt;code&gt;_time&lt;/code&gt;. &lt;br/&gt;Example: &lt;code&gt;DATEDIFF(second, &#39;1970-01-01&#39;, dateColumn) AS time&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeFilter(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Adds a time range filter for the specified column. &lt;br/&gt;Example: &lt;code&gt;dateColumn BETWEEN &#39;2017-04-21T05:01:17Z&#39; AND &#39;2017-04-21T05:06:17Z&#39;&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeFrom()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Returns the start of the current time range. &lt;br/&gt;Example: &lt;code&gt;&#39;2017-04-21T05:01:17Z&#39;&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeTo()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Returns the end of the current time range. &lt;br/&gt;Example: &lt;code&gt;&#39;2017-04-21T05:06:17Z&#39;&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn, &#39;5m&#39;[, fillValue])&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Groups the specified time column into intervals (e.g., 5 minutes). Optionally fills gaps with a value like &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;NULL&lt;/code&gt;, or &lt;code&gt;previous&lt;/code&gt;. &lt;br/&gt;Example: &lt;code&gt;CAST(ROUND(DATEDIFF(second, &#39;1970-01-01&#39;, time_column)/300.0, 0) AS bigint) * 300&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn, &#39;5m&#39;, 0)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as above, with &lt;code&gt;0&lt;/code&gt; used to fill missing data points.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn, &#39;5m&#39;, NULL)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as above, with &lt;code&gt;NULL&lt;/code&gt; used for missing data points.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn, &#39;5m&#39;, previous)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as above, using the previous value to fill gaps. If no previous value exists, &lt;code&gt;NULL&lt;/code&gt; is used.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroupAlias(dateColumn, &#39;5m&#39;)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as &lt;code&gt;$__timeGroup&lt;/code&gt;, but also adds an alias to the resulting column.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochFilter(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Adds a time range filter using Unix timestamps. &lt;br/&gt;Example: &lt;code&gt;dateColumn &amp;gt; 1494410783 AND dateColumn &amp;lt; 1494497183&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochFrom()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Returns the start of the current time range as a Unix timestamp. &lt;br/&gt;Example: &lt;code&gt;1494410783&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochTo()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Returns the end of the current time range as a Unix timestamp. &lt;br/&gt;Example: &lt;code&gt;1494497183&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochNanoFilter(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Adds a time range filter using nanosecond-precision Unix timestamps. &lt;br/&gt;Example: &lt;code&gt;dateColumn &amp;gt; 1494410783152415214 AND dateColumn &amp;lt; 1494497183142514872&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochNanoFrom()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Returns the start of the current time range as a nanosecond Unix timestamp. &lt;br/&gt;Example: &lt;code&gt;1494410783152415214&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochNanoTo()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Returns the end of the current time range as a nanosecond Unix timestamp. &lt;br/&gt;Example: &lt;code&gt;1494497183142514872&lt;/code&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochGroup(dateColumn, &#39;5m&#39;, [fillMode])&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as &lt;code&gt;$__timeGroup&lt;/code&gt;, but for Unix timestamps. Optional &lt;code&gt;fillMode&lt;/code&gt; controls how to handle missing points.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochGroupAlias(dateColumn, &#39;5m&#39;, [fillMode])&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as above, but adds an alias to the grouped column.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;view-the-interpolated-query&#34;&gt;View the interpolated query&lt;/h3&gt;
&lt;p&gt;The query editor includes a &lt;strong&gt;Generated SQL&lt;/strong&gt; link that appears after you run a query while editing a panel. Click this link to view the raw interpolated SQL that Grafana executed, including any macros that were expanded during query processing.&lt;/p&gt;
&lt;h2 id=&#34;table-queries&#34;&gt;Table queries&lt;/h2&gt;
&lt;p&gt;To create a Table query, set the &lt;strong&gt;Format&lt;/strong&gt; option in the query editor to 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/visualizations/table/&#34;&gt;&lt;strong&gt;Table&lt;/strong&gt;&lt;/a&gt;. This allows you to write any valid SQL query, and the Table panel will display the results using the returned columns and rows.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;CREATE TABLE [event] (
  time_sec bigint,
  description nvarchar(100),
  tags nvarchar(100),
)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;CREATE TABLE [mssql_types] (
  c_bit bit, c_tinyint tinyint, c_smallint smallint, c_int int, c_bigint bigint, c_money money, c_smallmoney smallmoney, c_numeric numeric(10,5),
  c_real real, c_decimal decimal(10,2), c_float float,
  c_char char(10), c_varchar varchar(10), c_text text,
  c_nchar nchar(12), c_nvarchar nvarchar(12), c_ntext ntext,
  c_datetime datetime,  c_datetime2 datetime2, c_smalldatetime smalldatetime, c_date date, c_time time, c_datetimeoffset datetimeoffset
)

INSERT INTO [mssql_types]
SELECT
  1, 5, 20020, 980300, 1420070400, &amp;#39;$20000.15&amp;#39;, &amp;#39;£2.15&amp;#39;, 12345.12,
  1.11, 2.22, 3.33,
  &amp;#39;char10&amp;#39;, &amp;#39;varchar10&amp;#39;, &amp;#39;text&amp;#39;,
  N&amp;#39;☺nchar12☺&amp;#39;, N&amp;#39;☺nvarchar12☺&amp;#39;, N&amp;#39;☺text☺&amp;#39;,
  GETDATE(), CAST(GETDATE() AS DATETIME2), CAST(GETDATE() AS SMALLDATETIME), CAST(GETDATE() AS DATE), CAST(GETDATE() AS TIME), SWITCHOFFSET(CAST(GETDATE() AS DATETIMEOFFSET), &amp;#39;-07:00&amp;#39;)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Example query with output:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT * FROM [mssql_types]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow&#34;
    style=&#34;max-width: 500px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/static/img/docs/v51/mssql_table_query.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/static/img/docs/v51/mssql_table_query.png&#34;data-srcset=&#34;/static/img/docs/v51/mssql_table_query.png?w=320 320w, /static/img/docs/v51/mssql_table_query.png?w=550 550w, /static/img/docs/v51/mssql_table_query.png?w=750 750w, /static/img/docs/v51/mssql_table_query.png?w=900 900w, /static/img/docs/v51/mssql_table_query.png?w=1040 1040w, /static/img/docs/v51/mssql_table_query.png?w=1240 1240w, /static/img/docs/v51/mssql_table_query.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;&#34;width=&#34;559&#34;height=&#34;336&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/static/img/docs/v51/mssql_table_query.png&#34;
            alt=&#34;&#34;width=&#34;559&#34;height=&#34;336&#34;class=&#34;docs-image--no-shadow&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;Use the keyword &lt;code&gt;AS&lt;/code&gt; to define an alias in your query to rename a column or table.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example query with output:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  c_bit AS [column1], c_tinyint AS [column2]
FROM
  [mssql_types]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow&#34;
    style=&#34;max-width: 1489px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/static/img/docs/v51/mssql_table_result.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/static/img/docs/v51/mssql_table_result.png&#34;data-srcset=&#34;/static/img/docs/v51/mssql_table_result.png?w=320 320w, /static/img/docs/v51/mssql_table_result.png?w=550 550w, /static/img/docs/v51/mssql_table_result.png?w=750 750w, /static/img/docs/v51/mssql_table_result.png?w=900 900w, /static/img/docs/v51/mssql_table_result.png?w=1040 1040w, /static/img/docs/v51/mssql_table_result.png?w=1240 1240w, /static/img/docs/v51/mssql_table_result.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;&#34;width=&#34;2013&#34;height=&#34;167&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/static/img/docs/v51/mssql_table_result.png&#34;
            alt=&#34;&#34;width=&#34;2013&#34;height=&#34;167&#34;class=&#34;docs-image--no-shadow&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;h2 id=&#34;time-series-queries&#34;&gt;Time series queries&lt;/h2&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Store timestamps in UTC to avoid issues with time shifts in Grafana when using non-UTC timezones.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;To create a time series query, set the &lt;strong&gt;Format&lt;/strong&gt; option in the query editor to &lt;strong&gt;Time series&lt;/strong&gt;. The query must include a column named &lt;code&gt;time&lt;/code&gt;, which should contain either a SQL &lt;code&gt;datetime&lt;/code&gt; value or a numeric value representing Unix epoch time in seconds. The result set must be sorted by the &lt;code&gt;time&lt;/code&gt; column for panels to visualize the data correctly.&lt;/p&gt;
&lt;p&gt;A time series query returns results&lt;a href=&#34;/developers/plugin-tools/key-concepts/data-frames#wide-format&#34;&gt;wide data frame format&lt;/a&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Any column except &lt;code&gt;time&lt;/code&gt; or of the type &lt;code&gt;string&lt;/code&gt; transforms into value fields in the data frame query result.&lt;/li&gt;
&lt;li&gt;Any string column transforms into field labels in the data frame query result.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can enable macro support in the &lt;code&gt;SELECT&lt;/code&gt; clause to create time series queries more easily. Use the &lt;strong&gt;Data operations&lt;/strong&gt; drop-down to choose a macro such as &lt;code&gt;$\_\_timeGroup&lt;/code&gt; or &lt;code&gt;$\_\_timeGroupAlias&lt;/code&gt;, then select a time column from the Column drop-down and a time interval from the Interval drop-down. This generates a time-series query based on your selected time grouping.&lt;/p&gt;


&lt;div data-shared=&#34;datasources/sql-query-builder-macros.md&#34;&gt;
            &lt;h4 id=&#34;macros&#34;&gt;Macros&lt;/h4&gt;
&lt;p&gt;You can enable macros support in the select clause to create time-series queries.&lt;/p&gt;
&lt;p&gt;Use the &lt;strong&gt;Data operations&lt;/strong&gt; drop-down to select a macro like &lt;code&gt;$__timeGroup&lt;/code&gt; or &lt;code&gt;$__timeGroupAlias&lt;/code&gt;.
Select a time column from the &lt;strong&gt;Column&lt;/strong&gt; drop-down and a time interval from the &lt;strong&gt;Interval&lt;/strong&gt; drop-down to create a time-series query.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow&#34;
    style=&#34;max-width: 2378px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link captioned&#34;
        href=&#34;/media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload mb-0&#34;
          data-src=&#34;/media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png&#34;data-srcset=&#34;/media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=320 320w, /media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=550 550w, /media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=750 750w, /media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=900 900w, /media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=1040 1040w, /media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=1240 1240w, /media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;SQL query builder time-series query&#34;width=&#34;2378&#34;height=&#34;1632&#34;title=&#34;SQL query builder time-series query&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/grafana/data-sources/screenshot-sql-builder-time-series-query.png&#34;
            alt=&#34;SQL query builder time-series query&#34;width=&#34;2378&#34;height=&#34;1632&#34;title=&#34;SQL query builder time-series query&#34;class=&#34;docs-image--no-shadow&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;figcaption class=&#34;w-100p caption text-gray-13  &#34;&gt;SQL query builder time-series query&lt;/figcaption&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;You can also add custom value to the &lt;strong&gt;Data operations&lt;/strong&gt;.
For example, a function that&amp;rsquo;s not in the drop-down list.
This allows you to add any number of parameters.&lt;/p&gt;
&lt;/div&gt;

        
&lt;h3 id=&#34;create-a-metric-query&#34;&gt;Create a metric query&lt;/h3&gt;
&lt;p&gt;For backward compatibility, there&amp;rsquo;s an exception to the above rule for queries that return three columns and include a string column named &lt;code&gt;metric&lt;/code&gt;.
Instead of transforming the &lt;code&gt;metric&lt;/code&gt; column into field labels, it becomes the field name, and then the series name is formatted as the value of the &lt;code&gt;metric&lt;/code&gt; column.
See the example with the &lt;code&gt;metric&lt;/code&gt; column below.&lt;/p&gt;
&lt;p&gt;To optionally customize the default series name formatting, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/configure-standard-options/#display-name&#34;&gt;Standard options definitions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example with &lt;code&gt;metric&lt;/code&gt; column:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(time_date_time, &amp;#39;5m&amp;#39;),
  min(&amp;#34;value_double&amp;#34;),
  &amp;#39;min&amp;#39; as metric
FROM test_data
WHERE $__timeFilter(time_date_time)
GROUP BY time
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Data frame result:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;-----------------&amp;#43;
| Name: time          | Name: min       |
| Labels:             | Labels:         |
| Type: []time.Time   | Type: []float64 |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;
| 2020-01-02 03:05:00 | 3               |
| 2020-01-02 03:10:00 | 6               |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;time-series-query-examples&#34;&gt;Time series query examples&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Use the fill parameter in the $__timeGroupAlias macro to convert null values to be zero instead:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(createdAt, &amp;#39;5m&amp;#39;, 0),
  sum(value) as value,
  hostname
FROM test_data
WHERE
  $__timeFilter(createdAt)
GROUP BY
  time,
  hostname
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Given the data frame result in the following example and using the graph panel, you will get two series named &lt;em&gt;value 10.0.1.1&lt;/em&gt; and &lt;em&gt;value 10.0.1.2&lt;/em&gt;. To render the series with a name of &lt;em&gt;10.0.1.1&lt;/em&gt; and &lt;em&gt;10.0.1.2&lt;/em&gt; , use a 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/configure-standard-options/#display-name&#34;&gt;Standard options definitions&lt;/a&gt; display name value of &lt;code&gt;${__field.labels.hostname}&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Data frame result:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;---------------------------&amp;#43;---------------------------&amp;#43;
| Name: time          | Name: value               | Name: value               |
| Labels:             | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 |
| Type: []time.Time   | Type: []float64           | Type: []float64           |
&amp;#43;---------------------&amp;#43;---------------------------&amp;#43;---------------------------&amp;#43;
| 2020-01-02 03:05:00 | 3                         | 4                         |
| 2020-01-02 03:10:00 | 6                         | 7                         |
&amp;#43;---------------------&amp;#43;---------------------------&amp;#43;---------------------------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Use multiple columns:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(time_date_time, &amp;#39;5m&amp;#39;),
  min(value_double) as min_value,
  max(value_double) as max_value
FROM test_data
WHERE $__timeFilter(time_date_time)
GROUP BY time
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Data frame result:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;-----------------&amp;#43;-----------------&amp;#43;
| Name: time          | Name: min_value | Name: max_value |
| Labels:             | Labels:         | Labels:         |
| Type: []time.Time   | Type: []float64 | Type: []float64 |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;-----------------&amp;#43;
| 2020-01-02 03:04:00 | 3               | 4               |
| 2020-01-02 03:05:00 | 6               | 7               |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;-----------------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;apply-annotations&#34;&gt;Apply annotations&lt;/h2&gt;
&lt;p&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/build-dashboards/annotate-visualizations/&#34;&gt;Annotations&lt;/a&gt; overlay rich event information on top of graphs.
You can add annotation queries in the Dashboard menu&amp;rsquo;s &lt;strong&gt;Annotations&lt;/strong&gt; view.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Columns:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;time&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;The name of the date/time field. Can be a column with a native SQL date/time data type or epoch value.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;timeend&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;&lt;em&gt;Optional&lt;/em&gt; name of the end date/time field. Can be a column with a native SQL date/time data type or epoch value.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;text&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Field containing the event description.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;tags&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;&lt;em&gt;Optional&lt;/em&gt; field used for event tags, formatted as a comma-separated string.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Example database tables:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;CREATE TABLE [events] (
  time_sec bigint,
  description nvarchar(100),
  tags nvarchar(100),
)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The following example also uses the database table defined in the &lt;a href=&#34;#time-series-queries&#34;&gt;Time series queries&lt;/a&gt; section.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example query using time column with epoch values:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  time_sec as time,
  description as [text],
  tags
FROM
  [events]
WHERE
  $__unixEpochFilter(time_sec)
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Example region query using time and timeend columns with epoch values:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  time_sec as time,
  time_end_sec as timeend,
  description as [text],
  tags
FROM
  [events]
WHERE
  $__unixEpochFilter(time_sec)
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Example query using time column of native SQL date/time data type:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  time,
  measurement as text,
  convert(varchar, valueOne) &amp;#43; &amp;#39;,&amp;#39; &amp;#43; convert(varchar, valueTwo) as tags
FROM
  metric_values
WHERE
  $__timeFilter(time_column)
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;use-stored-procedures&#34;&gt;Use stored procedures&lt;/h2&gt;
&lt;p&gt;Stored procedures have been verified to work with Grafana queries. However, note that there is no special handling or extended support for stored procedures, so some edge cases may not behave as expected.&lt;/p&gt;
&lt;p&gt;Stored procedures can be used in table, time series, and annotation queries, provided that the returned data matches the expected column names and formats described in the relevant previous sections in this document.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Grafana macro functions do not work inside stored procedures.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow docs-image--right&#34;
    style=&#34;max-width: 1054px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/static/img/docs/v51/mssql_metrics_graph.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/static/img/docs/v51/mssql_metrics_graph.png&#34;data-srcset=&#34;/static/img/docs/v51/mssql_metrics_graph.png?w=320 320w, /static/img/docs/v51/mssql_metrics_graph.png?w=550 550w, /static/img/docs/v51/mssql_metrics_graph.png?w=750 750w, /static/img/docs/v51/mssql_metrics_graph.png?w=900 900w, /static/img/docs/v51/mssql_metrics_graph.png?w=1040 1040w, /static/img/docs/v51/mssql_metrics_graph.png?w=1240 1240w, /static/img/docs/v51/mssql_metrics_graph.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;&#34;width=&#34;1054&#34;height=&#34;552&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/static/img/docs/v51/mssql_metrics_graph.png&#34;
            alt=&#34;&#34;width=&#34;1054&#34;height=&#34;552&#34;class=&#34;docs-image--no-shadow docs-image--right&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;For the following examples, the database table is defined in &lt;a href=&#34;#time-series-queries&#34;&gt;Time series queries&lt;/a&gt;. Let&amp;rsquo;s say that we want to visualize four series in a graph panel, such as all combinations of columns &lt;code&gt;valueOne&lt;/code&gt;, &lt;code&gt;valueTwo&lt;/code&gt; and &lt;code&gt;measurement&lt;/code&gt;. Graph panel to the right visualizes what we want to achieve. To solve this, you need to use two queries:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First query:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroup(time, &amp;#39;5m&amp;#39;) as time,
  measurement &amp;#43; &amp;#39; - value one&amp;#39; as metric,
  avg(valueOne) as valueOne
FROM
  metric_values
WHERE
  $__timeFilter(time)
GROUP BY
  $__timeGroup(time, &amp;#39;5m&amp;#39;),
  measurement
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Second query:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroup(time, &amp;#39;5m&amp;#39;) as time,
  measurement &amp;#43; &amp;#39; - value two&amp;#39; as metric,
  avg(valueTwo) as valueTwo
FROM
  metric_values
GROUP BY
  $__timeGroup(time, &amp;#39;5m&amp;#39;),
  measurement
ORDER BY 1&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;stored-procedure-with-epoch-time-format&#34;&gt;Stored procedure with epoch time format&lt;/h3&gt;
&lt;p&gt;You can define a stored procedure to return all the data needed to render multiple series (for example, 4) in a graph panel.&lt;/p&gt;
&lt;p&gt;In the following example, the stored procedure accepts two parameters, &lt;code&gt;@from&lt;/code&gt; and &lt;code&gt;@to&lt;/code&gt;, both of type &lt;code&gt;int&lt;/code&gt;. These parameters represent a time range (from–to) in epoch time format and are used to filter the results returned by the procedure.&lt;/p&gt;
&lt;p&gt;The query inside the procedure simulates the behavior of &lt;code&gt;$__timeGroup(time, &#39;5m&#39;)&lt;/code&gt; by grouping timestamps into 5-minute intervals. While the expressions for time grouping are somewhat verbose, they can be extracted into reusable SQL Server functions to simplify the procedure.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;CREATE PROCEDURE sp_test_epoch(
  @from int,
  @to 	int
)	AS
BEGIN
  SELECT
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
    measurement &amp;#43; &amp;#39; - value one&amp;#39; as metric,
    avg(valueOne) as value
  FROM
    metric_values
  WHERE
    time &amp;gt;= DATEADD(s, @from, &amp;#39;1970-01-01&amp;#39;) AND time &amp;lt;= DATEADD(s, @to, &amp;#39;1970-01-01&amp;#39;)
  GROUP BY
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
    measurement
  UNION ALL
  SELECT
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
    measurement &amp;#43; &amp;#39; - value two&amp;#39; as metric,
    avg(valueTwo) as value
  FROM
    metric_values
  WHERE
    time &amp;gt;= DATEADD(s, @from, &amp;#39;1970-01-01&amp;#39;) AND time &amp;lt;= DATEADD(s, @to, &amp;#39;1970-01-01&amp;#39;)
  GROUP BY
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
    measurement
  ORDER BY 1
END&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Then, in your graph panel, you can use the following query to call the stored procedure with the time range dynamically populated by Grafana:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;DECLARE
  @from int = $__unixEpochFrom(),
  @to int = $__unixEpochTo()

EXEC dbo.sp_test_epoch @from, @to&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This uses Grafana built-in macros to convert the selected time range into epoch time ($**unixEpochFrom() and $**unixEpochTo()), which are passed to the stored procedure as input parameters.&lt;/p&gt;
&lt;h3 id=&#34;stored-procedure-with-datetime-format&#34;&gt;Stored procedure with &lt;code&gt;datetime&lt;/code&gt; format&lt;/h3&gt;
&lt;p&gt;You can define a stored procedure to return all the data needed to render four series in a graph panel.&lt;/p&gt;
&lt;p&gt;In the following example, the stored procedure accepts two parameters, &lt;code&gt;@from&lt;/code&gt; and &lt;code&gt;@to&lt;/code&gt;, of the type &lt;code&gt;datetime&lt;/code&gt;. These parameters represent the selected time range and are used to filter the returned data.&lt;/p&gt;
&lt;p&gt;The query within the procedure mimics the behavior of &lt;code&gt;$__timeGroup(time, &#39;5m&#39;)&lt;/code&gt; by grouping data into 5-minute intervals. These expressions can be verbose, but you may extract them into reusable SQL Server functions for improved readability and maintainability.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;CREATE PROCEDURE sp_test_datetime(
  @from datetime,
  @to 	datetime
)	AS
BEGIN
  SELECT
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, time)/600 as int)*600 as int) as time,
    measurement &amp;#43; &amp;#39; - value one&amp;#39; as metric,
    avg(valueOne) as value
  FROM
    metric_values
  WHERE
    time &amp;gt;= @from AND time &amp;lt;= @to
  GROUP BY
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, time)/600 as int)*600 as int),
    measurement
  UNION ALL
  SELECT
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, time)/600 as int)*600 as int) as time,
    measurement &amp;#43; &amp;#39; - value two&amp;#39; as metric,
    avg(valueTwo) as value
  FROM
    metric_values
  WHERE
    time &amp;gt;= @from AND time &amp;lt;= @to
  GROUP BY
    cast(cast(DATEDIFF(second, {d &amp;#39;1970-01-01&amp;#39;}, time)/600 as int)*600 as int),
    measurement
  ORDER BY 1
END&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To call this stored procedure from a graph panel, use the following query with Grafana built-in macros to populate the time range dynamically:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;DECLARE
  @from datetime = $__timeFrom(),
  @to datetime = $__timeTo()

EXEC dbo.sp_test_datetime @from, @to&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="microsoft-sql-server-query-editor">Microsoft SQL Server query editor&lt;/h1>
&lt;p>Grafana provides a query editor for the Microsoft SQL Server data source, which is located on the
&lt;a href="/docs/grafana/v12.4/explore/">Explore page&lt;/a>. You can also access the MSSQL query editor from a dashboard panel. Click the menu in the upper right of the panel and select &lt;strong>Edit&lt;/strong>.&lt;/p></description></item><item><title>Microsoft SQL Server template variables</title><link>https://grafana.com/docs/grafana/v12.4/datasources/mssql/template-variables/</link><pubDate>Fri, 03 Apr 2026 19:43:06 +0000</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/mssql/template-variables/</guid><content><![CDATA[&lt;h1 id=&#34;microsoft-sql-server-template-variables&#34;&gt;Microsoft SQL Server template variables&lt;/h1&gt;
&lt;p&gt;Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
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 &lt;strong&gt;template variables&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For general information on using variables in Grafana, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/&#34;&gt;Add variables&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For an introduction to templating and template variables, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/&#34;&gt;Templating&lt;/a&gt; and 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/&#34;&gt;Add and manage variables&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;query-variable&#34;&gt;Query variable&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;For example, the following query returns all values from the &lt;code&gt;hostname&lt;/code&gt; column:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname FROM host&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;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 &lt;code&gt;hostname&lt;/code&gt; and &lt;code&gt;hostname2&lt;/code&gt; columns, which are included in the variable&amp;rsquo;s drop-down list.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT [host].[hostname], [other_host].[hostname2] FROM host JOIN other_host ON [host].[city] = [other_host].[city]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can also create a key/value variable using a query that returns two columns named &lt;code&gt;__text&lt;/code&gt; and &lt;code&gt;__value&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;__text&lt;/code&gt; column defines the label shown in the drop-down.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;__value&lt;/code&gt; column defines the value passed to panel queries.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is useful when you want to display a user-friendly label (like a hostname) but use a different underlying value (like an ID).&lt;/p&gt;
&lt;p&gt;Note that the values in the &lt;code&gt;_text&lt;/code&gt; column should be unique. If there are duplicates, Grafana uses only the first matching entry.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname __text, id __value FROM host&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can also create nested variables, where one variable depends on the value of another. For example, if you have a variable named &lt;code&gt;region&lt;/code&gt;, you can configure a &lt;code&gt;hosts&lt;/code&gt; variable to only show hosts from the selected region. If &lt;code&gt;region&lt;/code&gt; is a multi-value variable, use the &lt;code&gt;IN&lt;/code&gt; operator instead of &lt;code&gt;=&lt;/code&gt; to match against multiple selected values.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname FROM host WHERE region IN ($region)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;use-variables-in-queries&#34;&gt;Use variables in queries&lt;/h2&gt;
&lt;p&gt;Grafana automatically quotes template variable values only when the template variable is a &lt;code&gt;multi-value&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When using a multi-value variable, use the &lt;code&gt;IN&lt;/code&gt; comparison operator instead of &lt;code&gt;=&lt;/code&gt; to match against multiple values.&lt;/p&gt;
&lt;p&gt;Grafana supports two syntaxes for using variables in queries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;$&amp;lt;varname&amp;gt;&lt;/code&gt; syntax&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example with a template variable named &lt;code&gt;hostname&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  atimestamp time,
  aint value
FROM table
WHERE $__timeFilter(atimestamp) and hostname in($hostname)
ORDER BY atimestamp&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;[[varname]]&lt;/code&gt; syntax&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example with a template variable named &lt;code&gt;hostname&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  atimestamp as time,
  aint as value
FROM table
WHERE $__timeFilter(atimestamp) and hostname in([[hostname]])
ORDER BY atimestamp&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;disable-quoting-for-multi-value-variables&#34;&gt;Disable quoting for multi-value variables&lt;/h3&gt;
&lt;p&gt;By default, Grafana formats multi-value variables as a quoted, comma-separated string. For example, if &lt;code&gt;server01&lt;/code&gt; and &lt;code&gt;server02&lt;/code&gt; are selected, the result will be &lt;code&gt;&#39;server01&#39;&lt;/code&gt;, &lt;code&gt;&#39;server02&#39;&lt;/code&gt;. To disable quoting, use the &lt;code&gt;csv&lt;/code&gt; formatting option for variables:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;${servers:csv}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This outputs the values as an unquoted comma-separated list.&lt;/p&gt;
&lt;p&gt;Refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/variable-syntax/#advanced-variable-format-options&#34;&gt;Advanced variable format options&lt;/a&gt; for additional information.&lt;/p&gt;
]]></content><description>&lt;h1 id="microsoft-sql-server-template-variables">Microsoft SQL Server template variables&lt;/h1>
&lt;p>Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
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 &lt;strong>template variables&lt;/strong>.&lt;/p></description></item><item><title>Troubleshoot Microsoft SQL Server data source issues</title><link>https://grafana.com/docs/grafana/v12.4/datasources/mssql/troubleshooting/</link><pubDate>Fri, 03 Apr 2026 19:43:06 +0000</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/mssql/troubleshooting/</guid><content><![CDATA[&lt;h1 id=&#34;troubleshoot-microsoft-sql-server-data-source-issues&#34;&gt;Troubleshoot Microsoft SQL Server data source issues&lt;/h1&gt;
&lt;p&gt;This document provides solutions to common issues you may encounter when configuring or using the Microsoft SQL Server (MSSQL) data source in Grafana.&lt;/p&gt;
&lt;h2 id=&#34;connection-errors&#34;&gt;Connection errors&lt;/h2&gt;
&lt;p&gt;These errors occur when Grafana cannot establish or maintain a connection to the Microsoft SQL Server.&lt;/p&gt;
&lt;h3 id=&#34;unable-to-connect-to-the-server&#34;&gt;Unable to connect to the server&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Unable to open tcp connection&amp;rdquo; or &amp;ldquo;dial tcp: connection refused&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Grafana cannot establish a network connection to the SQL Server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify that the SQL Server is running and accessible.&lt;/li&gt;
&lt;li&gt;Check that the host and port are correct in the data source configuration. The default SQL Server port is &lt;code&gt;1433&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Ensure there are no firewall rules blocking the connection between Grafana and SQL Server.&lt;/li&gt;
&lt;li&gt;Verify that SQL Server is configured to allow remote connections.&lt;/li&gt;
&lt;li&gt;For Grafana Cloud, ensure you have configured &lt;a href=&#34;/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/&#34;&gt;Private data source connect&lt;/a&gt; if your SQL Server instance is not publicly accessible.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;connection-timeout&#34;&gt;Connection timeout&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Connection timed out&amp;rdquo; or &amp;ldquo;I/O timeout&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The connection to SQL Server timed out before receiving a response.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the network latency between Grafana and SQL Server.&lt;/li&gt;
&lt;li&gt;Verify that SQL Server is not overloaded or experiencing performance issues.&lt;/li&gt;
&lt;li&gt;Increase the &lt;strong&gt;Connection timeout&lt;/strong&gt; setting in the data source configuration under &lt;strong&gt;Additional settings&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Check if any network devices (load balancers, proxies) are timing out the connection.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;encryption-related-connection-failures&#34;&gt;Encryption-related connection failures&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;TLS handshake failed&amp;rdquo; or &amp;ldquo;certificate verify failed&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; There is a mismatch between the encryption settings in Grafana and what the SQL Server supports or requires.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For older versions of SQL Server (2008, 2008R2), set the &lt;strong&gt;Encrypt&lt;/strong&gt; option to &lt;strong&gt;Disable&lt;/strong&gt; or &lt;strong&gt;False&lt;/strong&gt; in the data source configuration.&lt;/li&gt;
&lt;li&gt;Verify that the SQL Server has a valid SSL certificate if encryption is enabled.&lt;/li&gt;
&lt;li&gt;Check that the certificate is trusted by the Grafana server.&lt;/li&gt;
&lt;li&gt;Ensure you&amp;rsquo;re using the latest available service pack for your SQL Server version for optimal compatibility.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;named-instance-connection-issues&#34;&gt;Named instance connection issues&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Cannot connect to named instance&amp;rdquo; or connection fails when using instance name&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Grafana cannot resolve the SQL Server named instance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the format &lt;code&gt;hostname\instancename&lt;/code&gt; or &lt;code&gt;hostname\instancename,port&lt;/code&gt; in the &lt;strong&gt;Host&lt;/strong&gt; field.&lt;/li&gt;
&lt;li&gt;Verify that the SQL Server Browser service is running on the SQL Server machine.&lt;/li&gt;
&lt;li&gt;If the Browser service is unavailable, specify the port number directly: &lt;code&gt;hostname,port&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check that UDP port 1434 is open if using the SQL Server Browser service.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;authentication-errors&#34;&gt;Authentication errors&lt;/h2&gt;
&lt;p&gt;These errors occur when there are issues with authentication credentials or permissions.&lt;/p&gt;
&lt;h3 id=&#34;login-failed-for-user&#34;&gt;Login failed for user&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Login failed for user &amp;lsquo;username&amp;rsquo;&amp;rdquo; or &amp;ldquo;Authentication failed&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The authentication credentials are invalid or the user doesn&amp;rsquo;t have permission to access the database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify that the username and password are correct.&lt;/li&gt;
&lt;li&gt;Check that the user exists in SQL Server and is enabled.&lt;/li&gt;
&lt;li&gt;Ensure the user has access to the specified database.&lt;/li&gt;
&lt;li&gt;For Windows Authentication, verify that the credentials are in the correct format (&lt;code&gt;DOMAIN\User&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Check that the SQL Server authentication mode allows the type of login you&amp;rsquo;re using (SQL Server Authentication, Windows Authentication, or Mixed Mode).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;access-denied-to-database&#34;&gt;Access denied to database&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Cannot open database &amp;lsquo;dbname&amp;rsquo; requested by the login&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The authenticated user doesn&amp;rsquo;t have permission to access the specified database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Verify that the database name is correct in the data source configuration.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Ensure the user is mapped to the database with appropriate permissions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Grant at least &lt;code&gt;SELECT&lt;/code&gt; permission on the required tables:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;USE [your_database]
GRANT SELECT ON dbo.YourTable TO [your_user]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Check that the user doesn&amp;rsquo;t have any conflicting permissions from the public role.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;windows-authentication-kerberos-issues&#34;&gt;Windows Authentication (Kerberos) issues&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Kerberos authentication failed&amp;rdquo; or &amp;ldquo;Cannot initialize Kerberos&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Kerberos configuration is incorrect or incomplete.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify that the Kerberos configuration file (&lt;code&gt;krb5.conf&lt;/code&gt;) path is correct in the data source settings.&lt;/li&gt;
&lt;li&gt;For keytab authentication, ensure the keytab file exists and is readable by Grafana.&lt;/li&gt;
&lt;li&gt;Check that the realm and KDC settings are correct in the Kerberos configuration.&lt;/li&gt;
&lt;li&gt;Verify DNS is correctly resolving the KDC servers.&lt;/li&gt;
&lt;li&gt;Ensure the service principal name (SPN) is registered for the SQL Server instance.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Kerberos authentication is not supported in Grafana Cloud.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h3 id=&#34;azure-entra-id-authentication-errors&#34;&gt;Azure Entra ID authentication errors&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;AADSTS error codes&amp;rdquo; or &amp;ldquo;Azure AD authentication failed&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Azure Entra ID (formerly Azure AD) authentication is misconfigured.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;For &lt;strong&gt;App Registration&lt;/strong&gt; authentication:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Verify the tenant ID, client ID, and client secret are correct.&lt;/li&gt;
&lt;li&gt;Ensure the app registration has been added as a user in the Azure SQL database.&lt;/li&gt;
&lt;li&gt;Check that the client secret hasn&amp;rsquo;t expired.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For &lt;strong&gt;Managed Identity&lt;/strong&gt; authentication:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Verify &lt;code&gt;managed_identity_enabled = true&lt;/code&gt; is set in the Grafana server configuration.&lt;/li&gt;
&lt;li&gt;Ensure the managed identity has been added to the Azure SQL database.&lt;/li&gt;
&lt;li&gt;Confirm the Azure resource hosting Grafana has managed identity enabled.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For &lt;strong&gt;Current User&lt;/strong&gt; authentication:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ensure &lt;code&gt;user_identity_enabled = true&lt;/code&gt; is set in the Grafana server configuration.&lt;/li&gt;
&lt;li&gt;Verify the app registration is configured to issue both Access Tokens and ID Tokens.&lt;/li&gt;
&lt;li&gt;Check that the required API permissions are configured (&lt;code&gt;user_impersonation&lt;/code&gt; for Azure SQL).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For detailed Azure authentication configuration, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/mssql/configure/&#34;&gt;Configure the Microsoft SQL Server data source&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;query-errors&#34;&gt;Query errors&lt;/h2&gt;
&lt;p&gt;These errors occur when there are issues with query syntax or configuration.&lt;/p&gt;
&lt;h3 id=&#34;time-column-not-found-or-invalid&#34;&gt;Time column not found or invalid&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Could not find time column&amp;rdquo; or time series visualization shows no data&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The query doesn&amp;rsquo;t return a properly formatted &lt;code&gt;time&lt;/code&gt; column for time series visualization.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ensure your query includes a column named &lt;code&gt;time&lt;/code&gt; when using the &lt;strong&gt;Time series&lt;/strong&gt; format.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;$__time()&lt;/code&gt; macro to rename your date column: &lt;code&gt;$__time(your_date_column)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Verify the time column is of a valid SQL date/time type (&lt;code&gt;datetime&lt;/code&gt;, &lt;code&gt;datetime2&lt;/code&gt;, &lt;code&gt;date&lt;/code&gt;) or contains Unix epoch values.&lt;/li&gt;
&lt;li&gt;Ensure the result set is sorted by the time column using &lt;code&gt;ORDER BY&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;macro-expansion-errors&#34;&gt;Macro expansion errors&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Error parsing query&amp;rdquo; or macros appear unexpanded in the query&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Grafana macros are being used incorrectly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify macro syntax: use &lt;code&gt;$__timeFilter(column)&lt;/code&gt; not &lt;code&gt;$_timeFilter(column)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Macros don&amp;rsquo;t work inside stored procedures—use explicit date parameters instead.&lt;/li&gt;
&lt;li&gt;Check that the column name passed to macros exists in your table.&lt;/li&gt;
&lt;li&gt;View the expanded query by clicking &lt;strong&gt;Generated SQL&lt;/strong&gt; after running the query to debug macro expansion.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;timezone-and-time-shift-issues&#34;&gt;Timezone and time shift issues&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Time series data appears shifted or doesn&amp;rsquo;t align with expected times.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Store timestamps in UTC in your database to avoid timezone issues.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Time macros (&lt;code&gt;$__time&lt;/code&gt;, &lt;code&gt;$__timeFilter&lt;/code&gt;, etc.) always expand to UTC values.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If your timestamps are stored in local time, convert them to UTC in your query:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  your_datetime_column AT TIME ZONE &amp;#39;Your Local Timezone&amp;#39; AT TIME ZONE &amp;#39;UTC&amp;#39; AS time,
  value
FROM your_table&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Don&amp;rsquo;t pass timezone parameters to time macros—they&amp;rsquo;re not supported.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;query-returns-too-many-rows&#34;&gt;Query returns too many rows&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Result set too large&amp;rdquo; or browser becomes unresponsive&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The query returns more data than can be efficiently processed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Add time filters using &lt;code&gt;$__timeFilter(column)&lt;/code&gt; to limit data to the dashboard time range.&lt;/li&gt;
&lt;li&gt;Use aggregations (&lt;code&gt;AVG&lt;/code&gt;, &lt;code&gt;SUM&lt;/code&gt;, &lt;code&gt;COUNT&lt;/code&gt;) with &lt;code&gt;GROUP BY&lt;/code&gt; instead of returning raw rows.&lt;/li&gt;
&lt;li&gt;Add a &lt;code&gt;TOP&lt;/code&gt; clause to limit results: &lt;code&gt;SELECT TOP 1000 ...&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;$__timeGroup()&lt;/code&gt; macro to aggregate data into time intervals.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;stored-procedure-returns-no-data&#34;&gt;Stored procedure returns no data&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Stored procedure output isn&amp;rsquo;t being captured correctly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ensure the stored procedure uses &lt;code&gt;SELECT&lt;/code&gt; statements, not just variable assignments.&lt;/li&gt;
&lt;li&gt;Remove &lt;code&gt;SET NOCOUNT ON&lt;/code&gt; if present, or ensure it&amp;rsquo;s followed by a &lt;code&gt;SELECT&lt;/code&gt; statement.&lt;/li&gt;
&lt;li&gt;Verify the stored procedure parameters are being passed correctly.&lt;/li&gt;
&lt;li&gt;Test the stored procedure directly in SQL Server Management Studio with the same parameters.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For more information on using stored procedures, refer to the 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/mssql/query-editor/&#34;&gt;query editor documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;performance-issues&#34;&gt;Performance issues&lt;/h2&gt;
&lt;p&gt;These issues relate to slow queries or high resource usage.&lt;/p&gt;
&lt;h3 id=&#34;slow-query-execution&#34;&gt;Slow query execution&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Queries take a long time to execute.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reduce the dashboard time range to limit data volume.&lt;/li&gt;
&lt;li&gt;Add indexes to columns used in &lt;code&gt;WHERE&lt;/code&gt; clauses and time filters.&lt;/li&gt;
&lt;li&gt;Use aggregations instead of returning individual rows.&lt;/li&gt;
&lt;li&gt;Increase the &lt;strong&gt;Min time interval&lt;/strong&gt; setting to reduce the number of data points.&lt;/li&gt;
&lt;li&gt;Review the query execution plan in SQL Server Management Studio to identify bottlenecks.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;connection-pool-exhaustion&#34;&gt;Connection pool exhaustion&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;Too many connections&amp;rdquo; or &amp;ldquo;Connection pool exhausted&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Too many concurrent connections to the database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Increase the &lt;strong&gt;Max open&lt;/strong&gt; connection limit in the data source configuration.&lt;/li&gt;
&lt;li&gt;Enable &lt;strong&gt;Auto max idle&lt;/strong&gt; to automatically manage idle connections.&lt;/li&gt;
&lt;li&gt;Reduce the number of panels querying the same data source simultaneously.&lt;/li&gt;
&lt;li&gt;Check for long-running queries that might be holding connections.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;other-common-issues&#34;&gt;Other common issues&lt;/h2&gt;
&lt;p&gt;The following issues don&amp;rsquo;t produce specific error messages but are commonly encountered.&lt;/p&gt;
&lt;h3 id=&#34;system-databases-appear-in-queries&#34;&gt;System databases appear in queries&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Queries accidentally access system databases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The query editor automatically excludes &lt;code&gt;tempdb&lt;/code&gt;, &lt;code&gt;model&lt;/code&gt;, &lt;code&gt;msdb&lt;/code&gt;, and &lt;code&gt;master&lt;/code&gt; from the database dropdown.&lt;/li&gt;
&lt;li&gt;Always specify the database in your data source configuration to restrict access.&lt;/li&gt;
&lt;li&gt;Ensure the database user only has permissions on the intended database.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;template-variable-queries-fail&#34;&gt;Template variable queries fail&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Variable queries return unexpected results or errors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the variable query syntax is valid SQL that returns a single column.&lt;/li&gt;
&lt;li&gt;Check that the data source connection is working.&lt;/li&gt;
&lt;li&gt;Ensure the user has permission to access the tables referenced in the variable query.&lt;/li&gt;
&lt;li&gt;Test the query in the query editor before using it as a variable query.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;data-appears-incorrect-or-misaligned&#34;&gt;Data appears incorrect or misaligned&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Data formatting or type conversion issues.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use explicit column aliases to ensure consistent naming: &lt;code&gt;SELECT value AS metric&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Verify numeric columns are actually numeric types, not strings.&lt;/li&gt;
&lt;li&gt;Check for &lt;code&gt;NULL&lt;/code&gt; values that might affect aggregations.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;FILL&lt;/code&gt; option in &lt;code&gt;$__timeGroup()&lt;/code&gt; macro to handle missing data points.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;get-additional-help&#34;&gt;Get additional help&lt;/h2&gt;
&lt;p&gt;If you continue to experience issues after following this troubleshooting guide:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the &lt;a href=&#34;https://community.grafana.com/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana community forums&lt;/a&gt; for similar issues.&lt;/li&gt;
&lt;li&gt;Review the &lt;a href=&#34;https://github.com/grafana/grafana/issues&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana GitHub issues&lt;/a&gt; for known bugs.&lt;/li&gt;
&lt;li&gt;Enable debug logging in Grafana to capture detailed error information.&lt;/li&gt;
&lt;li&gt;Check SQL Server logs for additional error details.&lt;/li&gt;
&lt;li&gt;Contact &lt;a href=&#34;/contact/&#34;&gt;Grafana Support&lt;/a&gt; if you&amp;rsquo;re an Enterprise or Cloud customer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When reporting issues, include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Grafana version&lt;/li&gt;
&lt;li&gt;SQL Server version&lt;/li&gt;
&lt;li&gt;Error messages (redact sensitive information)&lt;/li&gt;
&lt;li&gt;Steps to reproduce&lt;/li&gt;
&lt;li&gt;Relevant query examples (redact sensitive data)&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="troubleshoot-microsoft-sql-server-data-source-issues">Troubleshoot Microsoft SQL Server data source issues&lt;/h1>
&lt;p>This document provides solutions to common issues you may encounter when configuring or using the Microsoft SQL Server (MSSQL) data source in Grafana.&lt;/p></description></item></channel></rss>