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.

61 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!