sys.dm_exec_sql_text (Transact-SQL)

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008)yesAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

Returns the text of the SQL batch that is identified by the specified sql_handle. This table-valued function replaces the system function fn_get_sql.


sys.dm_exec_sql_text(sql_handle | plan_handle)  


Is the SQL handle of the batch to be looked up. sql_handle is varbinary(64). sql_handle can be obtained from the following dynamic management objects:

Table Returned

Column name Data type Description
dbid smallint ID of database.

For ad hoc and prepared SQL statements, the ID of the database where the statements were compiled.
objectid int ID of object.

Is NULL for ad hoc and prepared SQL statements.
number smallint For a numbered stored procedure, this column returns the number of the stored procedure. For more information, see sys.numbered_procedures (Transact-SQL).

Is NULL for ad hoc and prepared SQL statements.
encrypted bit 1 = SQL text is encrypted.

0 = SQL text is not encrypted.
text nvarchar(max ) Text of the SQL query.

Is NULL for encrypted objects.


Requires VIEW SERVER STATE permission on the server.


For batches, the SQL handles are hash values based on the SQL text. For database objects such as stored procedures, triggers or functions, the SQL handles are derived from the database ID, object ID, and object number. plan_handle is a hash value derived from the compiled plan of the entire batch.


A. Conceptual Example

The following is a basic example to illustrate passing a sql_handle either directly or with CROSS APPLY.

  1. Create activity.
    Execute the following T-SQL in a new query window in SQL Server Management Studio.

    -- Identify current spid (session_id)
    -- Create activity
    WAITFOR DELAY '00:02:00';
    1. Using CROSS APPLY.
      The sql_handle from sys.dm_exec_requests will be passed to sys.dm_exec_sql_text using CROSS APPLY. Open a new query window and pass the spid identified in step 1. In this example the spid happens to be 59.

      SELECT t.*
      FROM sys.dm_exec_requests AS r
      CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
      WHERE session_id = 59 -- modify this value with your actual spid
    2. Passing sql_handle directly.
      Acquire the sql_handle from sys.dm_exec_requests. Then, pass the sql_handle directly to sys.dm_exec_sql_text. Open a new query window and pass the spid identified in step 1 to sys.dm_exec_requests. In this example the spid happens to be 59. Then pass the returned sql_handle as an argument to sys.dm_exec_sql_text.

      -- acquire sql_handle
      SELECT sql_handle FROM sys.dm_exec_requests WHERE session_id = 59  -- modify this value with your actual spid
      -- pass sql_handle to sys.dm_exec_sql_text
      SELECT * FROM sys.dm_exec_sql_text(0x01000600B74C2A1300D2582A2100000000000000000000000000000000000000000000000000000000000000) -- modify this value with your actual sql_handle

B. Obtain information about the top five queries by average CPU time

The following example returns the text of the SQL statement and average CPU time for the top five queries.

SELECT TOP 5 total_worker_time/execution_count AS [Avg CPU Time],  
    SUBSTRING(st.text, (qs.statement_start_offset/2)+1,   
        ((CASE qs.statement_end_offset  
          WHEN -1 THEN DATALENGTH(st.text)  
         ELSE qs.statement_end_offset  
         END - qs.statement_start_offset)/2) + 1) AS statement_text  
FROM sys.dm_exec_query_stats AS qs  
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st  
ORDER BY total_worker_time/execution_count DESC;  

C. Provide batch-execution statistics

The following example returns the text of SQL queries that are being executed in batches and provides statistical information about them.

SELECT s2.dbid,   
    (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 ,   
      ( (CASE WHEN statement_end_offset = -1   
         THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2)   
         ELSE statement_end_offset END)  - statement_start_offset) / 2+1))  AS sql_statement,  
FROM sys.dm_exec_query_stats AS s1   
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2    
WHERE s2.objectid is null   
ORDER BY s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset;  

See also

Dynamic Management Views and Functions (Transact-SQL)
Execution Related Dynamic Management Views and Functions (Transact-SQL)
sys.dm_exec_query_stats (Transact-SQL)
sys.dm_exec_requests (Transact-SQL)
sys.dm_exec_cursors (Transact-SQL)
sys.dm_exec_xml_handles (Transact-SQL)
sys.dm_exec_query_memory_grants (Transact-SQL)