Skip to content

Commit

Permalink
add state, isreadonly to resultset
Browse files Browse the repository at this point in the history
  • Loading branch information
williamadba committed Aug 9, 2019
1 parent b00018d commit adfa3d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
64 changes: 34 additions & 30 deletions backup history oldest latest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
select
backuptype
, recovery_model_desc
, state_desc
, is_read_only
, OldestLatestBackupDate = MIN(BackupDate)
FROM
(
Expand All @@ -11,41 +13,42 @@ select
, d.recovery_model_desc
, BackupDate = MAX(BackupDate)
, d.state_desc
, d.is_read_only
from sys.databases d
inner join
(
select distinct
database_name
, backuptype = case type WHEN 'D' then 'Database'
WHEN 'I' then 'Differential database'
WHEN 'L' then 'Transaction Log'
WHEN 'F' then 'File or filegroup'
WHEN 'G' then 'Differential file'
WHEN 'P' then 'Partial'
WHEN 'Q' then 'Differential partial' END
, BackupDate = MAX(backup_start_date)
from msdb.dbo.backupset bs
group by database_name, type
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Database'
, null
(
select distinct
database_name
, backuptype = case type WHEN 'D' then 'Database'
WHEN 'I' then 'Differential database'
WHEN 'L' then 'Transaction Log'
WHEN 'F' then 'File or filegroup'
WHEN 'G' then 'Differential file'
WHEN 'P' then 'Partial'
WHEN 'Q' then 'Differential partial' END
, BackupDate = MAX(backup_start_date)
from msdb.dbo.backupset bs
group by database_name, type
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Database'
, null
FROM master.sys.databases d
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Transaction Log'
, null
FROM master.sys.databases d
UNION
select distinct
db_name(d.database_id)
, backuptype = 'Transaction Log'
, null
FROM master.sys.databases d
where d.recovery_model_desc in ('FULL', 'BULK_LOGGED')

) a
where d.recovery_model_desc in ('FULL', 'BULK_LOGGED')

) a
on db_name(d.database_id) = a.database_name
WHERE backuptype = 'transaction log'
group by database_name, backuptype, d.recovery_model_desc, d.state_desc
group by database_name, backuptype, d.recovery_model_desc, d.state_desc, d.is_read_only
) x
group by backuptype, recovery_model_desc
group by backuptype, recovery_model_desc, state_desc, is_read_only
order by backuptype, recovery_model_desc

GO
Expand All @@ -56,6 +59,7 @@ select
, d.recovery_model_desc
, BackupDate = MAX(BackupDate)
, d.state_desc
, d.is_read_only
from sys.databases d
inner join
(
Expand Down Expand Up @@ -88,4 +92,4 @@ select distinct
) a
on db_name(d.database_id) = a.database_name
--WHERE backuptype = 'transaction log'
group by database_name, backuptype, d.recovery_model_desc, d.state_desc
group by database_name, backuptype, d.recovery_model_desc, d.state_desc, d.is_read_only
4 changes: 3 additions & 1 deletion backup history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ select
, backup_size_mb = max(backup_size_mb)
, compressed_backup_size_mb = max(compressed_backup_size_mb)
, d.state_desc
, d.is_read_only
, d.create_date
from sys.databases d
inner join ( select * from (
Expand Down Expand Up @@ -60,7 +61,8 @@ group by
a.database_name
, a.backuptype
, d.recovery_model_desc
, d.state_desc
, d.state_desc
, d.is_read_only
, d.create_date
order by a.backuptype, d.recovery_model_desc, max(a.BackupFinishDate) asc, a.database_name asc
go
Expand Down

0 comments on commit adfa3d1

Please sign in to comment.