sql tsql__get_last_db_restores.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql tsql__get_last_db_restores.sql相关的知识,希望对你有一定的参考价值。


/*==========  GET RESTORE DATES  ==========*/

USE master
GO

/* SCRIPT 1 */

SELECT
	RowNum = ROW_NUMBER() OVER (PARTITION BY rsh.destination_database_name ORDER BY rsh.restore_date DESC)
	,rsh.destination_database_name
	,rsh.restore_date
	,rsh.[user_name]
	,bs.database_name [Source_DB_Name]
	,bmf.physical_device_name
FROM msdb.dbo.restorehistory rsh
JOIN msdb.dbo.backupset bs ON rsh.backup_set_id = bs.backup_set_id
JOIN msdb.dbo.restorefile rf ON rsh.restore_history_id = rf.restore_history_id
JOIN msdb.dbo.backupmediafamily bmf ON bs.media_set_id = bmf.media_set_id
WHERE
	rsh.restore_type = 'D'
	AND destination_database_name IN
						(
							'db_name'
						)
ORDER BY
	rsh.restore_date DESC


/* SCRIPT 2 */

;WITH LastRestores AS
(
SELECT
    DatabaseName = [d].[name] ,
    [d].[create_date] ,
    [d].[compatibility_level] ,
    [d].[collation_name] ,
    r.*,
    RowNum = ROW_NUMBER() OVER (PARTITION BY d.Name ORDER BY r.[restore_date] DESC)
FROM master.sys.databases d
LEFT OUTER JOIN msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.Name
)
SELECT *
FROM [LastRestores]
WHERE [RowNum] = 1
	 AND DatabaseName IN
				(
					'db_name'
				)

ORDER BY restore_date ASC


/* DB SIZES */

;WITH cte_db_sizes AS
(

SELECT
    DB_NAME( dbid ) AS DatabaseName,
    CAST( ( SUM( size ) * 8 ) / ( 1024.0 * 1024.0 ) AS decimal( 10, 2 ) ) AS DbSizeGb
FROM
    sys.sysaltfiles
GROUP BY
    DB_NAME( dbid )
)

SELECT *
FROM cte_db_sizes
WHERE DatabaseName IN
				(
					'db_name'
				)
ORDER BY DbSizeGb

以上是关于sql tsql__get_last_db_restores.sql的主要内容,如果未能解决你的问题,请参考以下文章

sql tsql__get_schema.sql

sql tsql__template_create_database.sql

sql tsql__SQLCMD_mode_check.sql

sql tsql__get_lock_history.sql

sql tsql__kill_idle_session.sql

sql tsql__kill_db_connections.sql