结合Newman和dylenv的前两条评论中的方法,我编写了一个基本脚本来将结果放入临时表中,以便可以根据需要对其进行过滤或操作。
declare
-- Table already multiplies percent by 100
-- Use the whole number percent
-- (e.g. 25 instead of 0.25)
@Percent_Used_threshold real = 25
drop table if exists ##log_space;
create table ##log_space (
database_id int primary key
, [db_name] sysname not null
, total_space_gb float not null
, used_space_gb float not null
, remaining_space_gb float not null
, used_log_space_in_percent real not null
);
declare
@sql varchar(max) = '
declare
@decimal_places int = 4
;
insert into
##log_space
select
db.database_id
, db.[name] as [db_name]
, round(
cast(log_space.total_log_size_in_bytes as float) / power(1024, 3)
, @decimal_places
) as total_space_gb
, round(
cast(log_space.used_log_space_in_bytes as float) / power(1024, 3)
, @decimal_places
) as used_space_gb
, round(
cast(log_space.total_log_size_in_bytes - log_space.used_log_space_in_bytes as float) / power(1024, 3)
, @decimal_places) as remaining_space_gb
, round(log_space.used_log_space_in_percent, @decimal_places) as used_log_space_in_percent
from
?.sys.dm_db_log_space_usage as log_space
inner join
?.sys.databases as db
on log_space.database_id = db.database_id
;'
exec sys.sp_MSforeachdb @sql
select
*
from
##log_space
where
used_log_space_in_percent >= @Percent_Used_threshold
order by
used_log_space_in_percent desc