Backing Up with Code in SQL Server
Hello everyone,
In this article, I will try to give information about backing up with code in SQL Server.
In SQL Server, in some cases, you may want to return with the backup code you have.
You can easily do this using the code below.
--Code 1 (restore backup via .bak file)
USE [master];
RESTORE DATABASE [DATABASENAME] --Where you specify the database name
FROM DISK = N'C:\database\backup\DATABASENAME.bak' --Here you choose the database path and database name
WITH FILE = 1,
NOUNLOAD,
REPLACE,
STATS = 5;
--Code 2 (To return a backup by specifying the location of the mdf and ldf files via the .bak file)
USE [master];
RESTORE DATABASE [DATABASENAME] --Where you specify the database name
FROM DISK = N'C:\database\backup\DATABASENAME.bak' --Here you choose the database path and database name
WITH FILE = 1,
MOVE 'DatabaseName' --Where you specify the database name
TO N'C:\Database\DATABASENAME.mdf', --You specify the .mdf path and name here
MOVE N'DATABASENAME_log'
TO N'C:\Database\DATABASENAME_log.ldf', --You specify the .ldf path and name here
NOUNLOAD,
REPLACE,
STATS = 5;
Good luck to everyone in business and life.