AGGREGATE功能组

时间:2016-08-31 20:44:11

标签: sql-server-2014

我正在尝试计算表的总数,总行数,数据库更新的最后时间以及上次执行存储过程的时间。手动计数后,我总共有4个表和153行。当我将sEPS.last_execution_time添加到SELECT的末尾时,它会抛出数字。有没有办法可以成功地AGGR所有内容,然后撤回上一个执行日期?

SELECT
  COUNT(SCHEMA_NAME(sO.schema_id)) AS TableCount,
  SUM(sPTN.Rows) AS [RowCount],
  MAX(sO.modify_date) AS LastUpdated,
  (sEPS.last_execution_time) AS 'LastExecuted'
FROM sys.objects AS sO
INNER JOIN sys.partitions AS sPTN ON sO.object_id = sPTN.object_id
INNER JOIN sys.dm_exec_procedure_stats AS sEPS ON sO.object_id = sEPS.object_id
WHERE sO.type = 'U'
GROUP BY sEPS.last_execution_time

当我运行上面的代码时,我得到5行(应该只有一行),我得到一个表3次。任何帮助表示赞赏。致谢

1 个答案:

答案 0 :(得分:1)

执行sp的最后一次无法连接到其余表,因为另一个表由object_id连接。你可以这样做:

SELECT  COUNT(DISTINCT SO.object_id) AS TableCount,
        SUM(sPTN.Rows) AS [RowCount],
        MAX(sO.modify_date) AS LastUpdated,
        MAX(LastExecuted) LastExecuted
FROM sys.objects AS sO
INNER JOIN sys.partitions AS sPTN 
    ON sO.object_id = sPTN.object_id
CROSS JOIN (SELECT MAX(last_execution_time) LastExecuted
            FROM sys.dm_exec_procedure_stats) AS sEPS 
WHERE sO.type = 'U';