Here is a script to restore SQL Server database from bak file command line. This is done via SQLCMD through command line.

The first thing is to open up the command prompt. Once command prompt is open, type in the below query to connect to instance.

C:\> SQLCMD -S localhost\DMSQL2014 -U sa -P sql@2014”

Once connected switch to master database as shown below

1>use master

The next step is to type in the below command to restore the database.

1> restore database Adventureworks2014 from disk=’e:\ahmad\adventureworks2014.bak’ with stats=10
2> GO

The database will be restored as shown in below snapshot.

1_restore sql server database from bak file command line

The other way is to do it without connecting to SQLCMD prompt and use –Q parameter of SQLCMD to specify the restore command as shown below.

2_restore sql server database from bak file command line

Another way is to specify a SQL Server script file in –I parameter. The SQL Server script file will have the restore database command.

3_restore sql server database from bak file command line

The file restore.sql has the restore database command. The file is given as input with –I parameter.

Happy learning!!!


Like us on FaceBook Join the fastest growing SQL Server group on FaceBook

Follow me on TwitterFollow me on FaceBook