SQL Server guide

Read dropdown labels and values


Resolve and list dropdown (choice) field labels and values for any ServiceNow table or form screen.

ServiceNow stores dropdown fields as internal values (for example 4) while displaying user-friendly labels (such as 4 - Low) in the UI. These mappings are defined in the sys_choice system table.

Use this example to discover and resolve dropdown options for fields like Priority, State, Category, Close Code, and any custom choice field across all ServiceNow tables.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

-- Resolve and list dropdown (choice) labels and values for any table and field
-- Replace <TABLE_NAME> and <FIELD_NAME> with your target form field (e.g. name=incident^element=priority)

SELECT *
FROM get_table_rows
WITH(
  TableName='sys_choice',
  Query='name=<TABLE_NAME>^element=<FIELD_NAME>'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SERVICENOW_IN_GATEWAY], '-- Resolve and list dropdown (choice) labels and values for any table and field
-- Replace <TABLE_NAME> and <FIELD_NAME> with your target form field (e.g. name=incident^element=priority)

SELECT *
FROM get_table_rows
WITH(
  TableName=''sys_choice'',
  Query=''name=<TABLE_NAME>^element=<FIELD_NAME>''
)')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_SERVICENOW_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = '-- Resolve and list dropdown (choice) labels and values for any table and field
-- Replace <TABLE_NAME> and <FIELD_NAME> with your target form field (e.g. name=incident^element=priority)

SELECT *
FROM get_table_rows
WITH(
  TableName=''sys_choice'',
  Query=''name=<TABLE_NAME>^element=<FIELD_NAME>''
)'
EXEC (@MyQuery) AT [LS_TO_SERVICENOW_IN_GATEWAY]