2016-08-31 24 views
0

我想計算我的總表數,總行數,最後一次數據庫已更新和上次執行存儲過程的時間。手動計數後,我總共有4個表格和153個行。當我將sEPS.last_execution_time添加到SELECT的末尾時,會拋出數字。有沒有辦法我可以成功AGGR的一切,然後拉回最後執行日期?AGGREGATE函數組通過

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

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'; 
+0

高十!謝謝拉瑪克 – Clint