简体   繁体   English

动态监视存储过程的执行和cpu-动态SQL上的错误

[英]dynamically monitor stored procedures executions and cpu - error on dynamic sql

I am developing a script to monitor stored procedures executions and cpu utilisation, however, running the script below dynamically it does not work. 我正在开发一个脚本来监视存储过程的执行和cpu使用率,但是,动态运行下面的脚本是行不通的。 It is working directly in SQL Server Mgmt Studio. 它直接在SQL Server Mgmt Studio中工作。 Can someone spot something that should be changed? 有人可以发现应该更改的内容吗?

The error message is: 错误消息是:

Msg 156, Level 15, State 1, Line 43 消息156,第15层,状态1,第43行
Incorrect syntax near the keyword 'proc'. 关键字“ proc”附近的语法不正确。

This is the script: 这是脚本:

DECLARE @SQL NVARCHAR(MAX)
--SELECT @SQL ='SELECT * FROM OPENQUERY('+ QUOTENAME('servername')    + ','''+ '
SELECT @SQL = 'SELECT dbID
                      ,dbname
                      ,StoredProcedure
                      ,last_execution_time
                      ,total_cpu_time
                      ,executionCounts
                      ,Total_Logical_Reads
                      ,Total_Logical_Writes 
               FROM OPENQUERY('+ QUOTENAME('servername') + ','''+ '

SET FMTONLY OFF   
SET NOCOUNT ON
SET DATEFORMAT DMY
SET DEADLOCK_PRIORITY LOW

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

;with r0 as 
(
   SELECT 
      st.dbID
      ,StoredProcedure = OBJECT_NAME(st.objectid,st.dbid)
      ,last_execution_time = MAX(QS.last_execution_time)
      ,total_cpu_time = sum(qs.total_worker_time) 
      ,executionCounts = sum(cp.usecounts)
      ,Total_Logical_Reads=sum(qs.Total_Logical_Reads)
      ,Total_Logical_Writes=sum(qs.Total_Logical_Writes)
   FROM 
      sys.dm_exec_query_stats qs 
   INNER JOIN 
      sys.dm_exec_cached_plans cp ON cp.plan_handle = qs.plan_handle
   CROSS APPLY 
      sys.dm_exec_sql_text(cp.plan_handle) st
   WHERE
      cp.objtype = ''proc''--> most of our problems are related to stored procedures 
      --  removing this filter would increase the cost of the query in the monitored servers
      AND OBJECT_NAME(st.objectid, st.dbid) IS NOT NULL
 GROUP BY
     st.dbid, st.objectid
)
SELECT 
    r0.dbID
    ,DBName = CASE WHEN r0.dbid = 32767 
                     THEN ''Resource''
                     ELSE COALESCE(DB_NAME(r0.dbid), ''No3 Name'') -- keep the coalesce for the database name to be used in a primary key constraint
              END 
    ,r0.StoredProcedure
    ,r0.last_execution_time
    ,r0.total_cpu_time
    ,r0.executionCounts
,r0.Total_Logical_Reads
,r0.Total_Logical_Writes
FROM  
    r0
ORDER BY 
    total_cpu_time DESC' + ''')'

--print @sql
EXEC SP_EXECUTESQL @SQL

This is one of the joys of dynamic SQL. 这是动态SQL的乐趣之一。

You're nesting a string in another string... 您正在将一个字符串嵌套在另一个字符串中...

Add another set of ' ' around the string literal 'proc' on line 43: 在第43行的字符串文字'proc'周围添加另一组'':

So: cp.objtype = ''proc'' 所以: cp.objtype = ''proc''

Becomes: cp.objtype = ''''proc'''' 成为: cp.objtype = ''''proc''''

A good test is comment out your EXEC and uncomment the PRINT line. 一个好的测试是注释掉您的EXEC并取消注释PRINT行。 Then run it to print the string to your window in SSMS. 然后运行它以将字符串打印到SSMS中的窗口。 Then copy the output to a new window... 然后将输出复制到新窗口...

Notice 'proc' and 'resource and 'No3 Name' are all escaped. 注意“ proc”和“ resource和'No3 Name”都被转义了。 You'll have to add extra ' to all of those literals to make it work. 您必须在所有这些文字中添加额外的'以使其起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM