SQL Server 查看数据表占用空间大小的SQL语句

时间:2021-01-06 18:43:50
 declare @name varchar(1000)
declare @sql varchar(1000) if object_id('tempdb..#space') is not null drop table #space
create table #space(name varchar(50),rows bigint,reserved varchar(12),data varchar(12),index_size varchar(12),unused varchar(12)) declare sp cursor local for select '['+name+']' from sysobjects where type = 'u'
open sp
fetch sp into @name
while @@fetch_status = 0
begin
set @sql = 'insert into #space exec sp_spaceused ' + @name
exec(@sql)
fetch sp into @name
end
close sp select * from #space order by cast(replace(reserved,'kb','')as int) desc

来自:SQL Server技术产品群(250942633)群友

本人独立博客:http://www.haoyuncn.net/sql-server-space