阿里云SQLServer存储空间已满,回收空间

今天发现SQLServer的存储空间已满,需要进行清除。

操作步骤:先清除表数据再回收空间

TRUNCATE TABLE 表名
DBCC CLEANTABLE (数据库名,表名, 0)
WITH NO_INFOMSGS;
GO

--1.查看数据库总空间,已用空间,未用空间,使用率
SELECT a.name as 逻辑文件名, size/128 [totalspace文件大小(兆)],
FILEPROPERTY(a.name, 'SpaceUsed')/128 [usedspace已用空间(兆)],
size/128 - FILEPROPERTY(a.name, 'SpaceUsed')/128 [未用空间(兆)],
FILEPROPERTY(a.name, 'SpaceUsed')*100.0/size [使用率(%)]
FROM sys.database_files a cross join (select recovery_model_desc, log_reuse_wait,log_reuse_wait_desc,is_auto_shrink_on from sys.databases where name=DB_NAME())b
WHERE type=0

--2.进行回收
declare @usedspace int ,@totalspace int
select @usedspace=已用空间 ,@totalspace =totalspace文件大小
while @totalspace> @usedspace
begin
set @totalspace= @totalspace-5*1024
DBCC SHRINKFILE(data1,@totalspace)
end
--DBCC(逻辑文件名,usedspace,totalspace)从1中的结果集获取

posted @ 2024-02-26 11:49  在你看不见的高空  阅读(14)  评论(0编辑  收藏  举报