Skip to main content

Datometry Documentation

Add a Custom Query Label to the OPTIONS Clause

You can add a custom query label to the Transact-SQL OPTION clause using the set_user_label hint. The label you specify is added to the LABEL statement.

You can use the OPTION clause in SELECT, DELETE, UPDATE and MERGE statements. To learn more, see the Microsoft article OPTION Clause (Transact-SQL).

A query label is a tag (or metadata) that you can apply to a query using the OPTION clause with the LABEL statement. This tag is helpful since the label is query-able and provides a mechanism for locating problem queries and can help to identify the progress of an ELT run.

SELECT
  --@dtm_set_user_label = MY_LABEL for query
  1;

SELECT
CAST ('1' AS smallint ) AS [1]
OPTION (LABEL = 'DTM_HASH:abd429e,DTM_BSID:c96.0001,USER_LABEL:MY_LABEL' )

You can then query the system management view sys.dm_pdw_exec_requests using the label you applied to retrieve information about the query.

SELECT  *
FROM    sys.dm_pdw_exec_requests r
WHERE   r.[label] like '%MY_LABEL%'
;

To learn more about query labels in Azure Synapse, see the Microsoft article Use query labels in Synapse SQL.

The syntax for set_user_label is:

set_user_label = user_label

where:

Parameter

Description

user_label

The label you want to specify for the statement.

Example 45. Specify a Custom Label for Individual SQL Statements

This example adds the label MY_LABEL to the LABEL statement of the the OPTION clause. When specified with the for query scope, the hint applies only to the statement in which it is embedded.

SELECT
  --@dtm_set_user_label = MY_LABEL for query
  1;

SELECT
CAST ('1' AS smallint ) AS [1]
OPTION (LABEL = 'DTM_HASH:abd429e,DTM_BSID:c96.0001,USER_LABEL:MY_LABEL' )


Example 46. Specify a Custom Label for the Duration of a Session

Using the for session hint, you can specify that set_user_label be used for the duration of a session, as opposed to a single request.

SELECT
  --@dtm_set_user_label = MY_LABEL for session
  1;