我可以在查询的Profiler中看到TextData吗?

时间:2019-04-30 06:59:34

标签: sql sql-server

当我执行sp_executesql过程时,探查器将显示调用它的查询。

例如)
执行

 exec sp_executesql N'EXEC [long_pro] @int = @p0', N'@p0 int', @p0=1

探查器

 EventClass        TextData
 SQL:BatchString   exec sp_executesql N'EXEC [long_pro] @int = @p0', N'@p0 int', @p0=1

但是,在dm_exec_sql_text中,将输出long_pro过程的生成语法。

CREATE PROCEDURE long_pro @int INT  AS    WAITFOR DELAY '00:01'  

有没有办法获取Profiler的TextData之类的查询?
以下查询是我当前正在使用的查询。

SELECT @m_RunTime                                                           AS RunTime
       ,@@SERVERNAME + '\' + @@SERVICENAME                                  AS ServerName
       ,DB_NAME(der.database_id)                                            AS DBName
       ,des.program_name                                                    AS ProgramName
       ,des.login_name                                                      AS LoginName
       ,CAST(DATEDIFF(ms, der.start_time, @runTime) / 1000 AS VARCHAR(10)) 
         + '.'
         + CAST(DATEDIFF(ms, der.start_time, @runTime) % 1000 AS VARCHAR(10)) AS Duration
       ,der.start_time                                                        AS StartTime
       ,der.cpu_time                                                          AS CPUTime
       ,der.reads                                                             AS Reads
       ,der.writes                                                            AS Writes
       /*
       ,SUBSTRING(dest.TEXT, (der.statement_start_offset / 2) + 1, ((CASE der.statement_end_offset WHEN -1     -- Text
                                                                                                   THEN DATALENGTH(dest.TEXT)
                                                                                                   ELSE der.statement_end_offset
                                                                                                   END - der.statement_start_offset) / 2) + 1) AS Text -- Text
                                                                                                   */
       ,dest.text text
FROM sys.dm_exec_requests der WITH(NOLOCK)
     INNER JOIN sys.dm_exec_sessions des WITH(NOLOCK)
       ON 1 = 1
          AND der.session_id = des.session_id
          AND der.session_id > 49
          AND der.session_id != @@SPID
          AND der.status IN ('running', 'runnable', 'suspended')
          AND der.start_time < DATEADD(second, @secTime, @runTime)
    CROSS APPLY sys.dm_exec_sql_text(sql_handle) dest
ORDER BY start_time ASC

0 个答案:

没有答案