Hanya untuk bersenang-senang, inilah pertanyaan yang akan menghasilkan data yang sama dengan laporan dalam jawaban nateirvin
create table #disk_usage
(
name varchar(128)
,rows varchar(20)
,reserved varchar(20)
,data varchar(20)
,index_size varchar(20)
,unused varchar(20)
);
exec sp_msforeachtable 'insert into #disk_usage exec sp_spaceused [?]'
select SCHEMA_NAME(st.schema_id) + '.' + du.name 'Table Name'
,du.rows '# Records'
,du.reserved 'Reserved (KB)'
,du.data 'Data (KB)'
,du.index_size 'Indexes (KB)'
,du.unused 'Unused (KB)'
from #disk_usage du
left join sys.tables st
on du.name = st.name
order by cast(left(reserved, len(reserved) - 3) as bigint) desc;
drop table #disk_usage
Oke, karena saya benar - benar membenci diri sendiri, saya menulis kueri yang akan menghasilkan hasil laporan, memformatnya sebagai tabel HTML, dan mengirimkannya sebagai email. Menyesuaikan warna latar belakang laporan dibiarkan sebagai latihan untuk pembaca.
declare @subject nvarchar(25) = 'Disk Usage by Top Tables';
declare @recipients nvarchar(25) = 'mailbox@example.com';
create table #disk_usage
(
name varchar(128)
,rows varchar(20)
,reserved varchar(20)
,data varchar(20)
,index_size varchar(20)
,unused varchar(20)
);
exec sp_msforeachtable 'insert into #disk_usage EXEC sp_spaceused [?]'
declare @body nvarchar(max) =
'<table cellspacing="0">
<thead>
<tr>
<th>Table Name</th>
<th># Rows</th>
<th>Reserved</th>
<th>Data</th>
<th>Indexes</th>
<th>Unused</th>
</tr>
</thead>
';
set @body = @body + cast (
(select '<td style="border: 1px solid black; padding: 2px">' + SCHEMA_NAME(s.schema_id) + '.' + t.name + '</td>'
,'<td style="border: 1px solid black; padding: 2px">' + rtrim(ltrim(t.rows)) + ' Rows </td>' -- for some reason this was generating a bunch of extra white space and I'm not going to bother to figure out why
,'<td style="border: 1px solid black; padding: 2px">' + t.reserved + '</td>'
,'<td style="border: 1px solid black; padding: 2px">' + t.data + '</td>'
,'<td style="border: 1px solid black; padding: 2px">' + t.index_size + '</td>'
,'<td style="border: 1px solid black; padding: 2px">' + t.unused + '</td>'
from #disk_usage t
left join sys.tables s
on t.name = s.name
order by cast(left(reserved, len(reserved) - 3) as bigint) desc
for xml path ('tr'))
as nvarchar(max));
set @body = replace(replace(@body, '<', '<'), '>', '>')
set @body = @body + '</table>'
exec msdb.dbo.sp_send_dbmail
@profile_name='A Database Mail Profile On The Target Server',
@recipients=@recipients,
@subject=@subject,
@body=@body,
@body_format='HTML'
drop table #disk_usage