-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQL003-AllTablesSpaceInInstance.sql
More file actions
59 lines (56 loc) · 1.83 KB
/
SQL003-AllTablesSpaceInInstance.sql
File metadata and controls
59 lines (56 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
-----------------------------------------------@kisinamso-----------------------------------------------
|If you want to learn all tables size in a Instance you can take this script. |
|You can acces to comments below. |
-----------------------------------------------@kisinamso-----------------------------------------------
*/
--Drop temp table if exists
DROP TABLE IF EXISTS #List
--Create a new temp table for return
CREATE TABLE #List
(
[DBName] VARCHAR(1000)
,[SchemaName] VARCHAR(1000)
,[TableName] VARCHAR(1000)
,[RowCount] BIGINT
,[TotalSpaceKB] BIGINT
,[TotalSpaceMB] BIGINT
,[UsedSpaceKB] BIGINT
,[UsedSpaceMB] BIGINT
,[UnusedSpaceKB] BIGINT
,[UnusedSpaceMB] BIGINT
)
GO
--Collect informations
INSERT INTO #List
EXEC sp_msforeachdb'Use [?]
SELECT
DB_NAME() AS DataBaseName,
s.Name AS SchemaName,
t.NAME AS TableName,
p.rows ,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
TotalSpaceMB DESC, t.Name'
--Select informations
SELECT * FROM #List ORDER BY DBName,[RowCount] DESC