查询某个数据库、以及其中每个数据表占据的存储空间大小,包括索引占的空间大小:

if not exists
(
   select * from dbo.sysobjects
   where id = object_id(N'[dbo].[tablespaceinfo]')
   and OBJECTPROPERTY(id, N'IsUserTable') = 1
)
create table tablespaceinfo
(
   nameinfo varchar(50) ,
   rowsinfo int , reserved varchar(20) ,
   datainfo varchar(20) ,
   index_size varchar(20) ,
   unused varchar(20)
)

delete from tablespaceinfo --清空原有数据表

declare @tablename varchar(255) 
declare @cmdsql varchar(500)
DECLARE Info_cursor CURSOR FOR  
select o.name  
from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1  
and o.name not like N'#%%' order by o.name

OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
if exists  
(
   select * from dbo.sysobjects
where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1
)
execute sp_executesql

N'insert into tablespaceinfo exec sp_spaceused @tbname',

N'@tbname varchar(255)',

@tbname = @tablename

FETCH NEXT FROM Info_cursor
INTO @tablename
END

CLOSE Info_cursor  --闭关游标

DEALLOCATE Info_cursor   --释放游标

GO

sp_spaceused @updateusage = 'TRUE'

select * from tablespaceinfo
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

查询所有数据库的恢复模式(Simple/Full),以及每个数据库的文件名、物理路径、存储空间、可用空间:

SELECT [name], recovery_model_desc, log_reuse_wait_desc FROM sys.databases;

SELECT name AS [File Name], file_id, 
physical_name AS [Physical Name],
size/128 AS [Total Size in MB],
size/128.0 - CAST(FILEPROPERTY(name,'SpaceUsed') AS int)/128.0AS [Available Space In MB]
FROM sys.database_files;

利用上面的方法找到log文件名以后,清楚log文件

dbcc shrinkfile( log文件名 , 1)