Posted On September 3, 2020

SQL: Backup Database and Purge It From SQL Server

kimconnect 0 comments
blog.KimConnect.com >> Database >> SQL: Backup Database and Purge It From SQL Server
/* Make a Final Backup of Database, Purge Its Backup and Restore History, and Remove Database */

/* Make a Final Backup */
BACKUP DATABASE DATABASE_NAME TO DISK = '\\ARCHIVE\Databases\DATABASENAME.bak' WITH COMPRESSION
GO

/* Purge Backup Chain Metadata */
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'DATABASENAME'
GO

/* Set Exclusive Access of SQL Server Database before Dropping It  */
/* Preempt this error:
Drop failed for Database 'DATABASENAME'. (Microsoft.SqlServer.Smo)
Cannot drop database "DATABASENAME" because it is currently in use. (Microsoft SQL Server, Error: 3702)
Cannot drop database because it is currently in use Microsoft SQL Server Error 3702
*/
USE [master]
GO
ALTER DATABASE [DATABASENAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [DATABASENAME] -- Now Delete the Database
GO

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

SQL: Truncate Transaction Logs

Overview: Significance: many small teams lack Database Admin resource to babysit SQL databases; therefore, certain…

SQL: Creating a Storage Report of Databases Residing within Server

Method 1: -- Get sizes of all databases from system (not real-time) WITH fs AS…

SQL Server Overview

SQL Buffer Manager (Cache) 8K Pages between memory and disk storage 8 Pages = 1…
Other project: DragonCoin.com