数据库
首页 > 数据库> > MSSQL查询数据库表数据大小等情况

MSSQL查询数据库表数据大小等情况

作者:互联网

1.查询数据库大小

SELECT 
    DB_NAME(database_id) AS '数据库名',
    [Name] AS '逻辑库名',
    [Physical_Name] AS '物理文件',
    ((size * 8) / 1024) AS '文件大小(MB)',
    [differential_base_time] AS '基准时间'
FROM sys.master_files
WHERE DB_NAME(database_id) like 'FuZhou%'

 

2.查询数据库表大小

 1 if exists(select 1 from tempdb..sysobjects where id=object_id('tempdb..#tabName') and xtype='u')
 2 drop table #tabName
 3 go
 4 create table #tabName(
 5 tabname varchar(100),
 6 rowsNum varchar(100),
 7 reserved varchar(100),
 8 data varchar(100),
 9 index_size varchar(100),
10 unused_size varchar(100)
11 )
12  
13 declare @name varchar(100)
14 declare cur cursor for
15 select name from sysobjects where xtype='u' order by name
16 open cur
17 fetch next from cur into @name
18 while @@fetch_status=0
19 begin
20     insert into #tabName
21     exec sp_spaceused @name
22     --print @name
23  
24     fetch next from cur into @name
25 end
26 close cur
27 deallocate cur
28 
29 select tabname as '表名',rowsNum as '表数据行数',reserved as '保留大小',data as '数据大小',index_size as '索引大小',unused_size as '未使用大小'
30 from #tabName
31 order by tabname asc, data desc

 

标签:varchar,name,tabName,查询数据库,大小,100,size,MSSQL,cur
来源: https://www.cnblogs.com/oumi/p/16423235.html