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
2>GO
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.
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.
Another way is to specify a SQL Server script file in –I parameter. The SQL Server script file will have the restore database command.
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
thank you . automated the script for all my database on nightly job.