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

Microsoft SQL: Shrink vs Truncate

Shrink The shrink command is to reduce the physical log file size of a database.…

How to Migrate a Database to a Newer Version of Microsoft SQL Server

Get Version of new SQL server Create a sql-dump script on the old SQL server:…

Enable Jumbo Frames on a Windows Host

Overview: Whether the engineer or sysadmin works in the realm of 'networking', 'database', or 'Windows',…