I have to restore around 60 SQL databases of different sizes. I googled to find a script to restore all databases after each other , just picking it 1 by 1 from my folder. I was not that successful, probably because I am quite new to SQL etc.
It can be either done in powershell or sql command line, what I found for now was something like this:
RESTORE DATABASE [db1] FROM DISK = N'C:\folder\db1.bak' WITH FILE = 1, NOUNLOAD, STATS = 10
GO
RESTORE DATABASE [db2] FROM DISK = N'C:\folder\db2.bak' WITH FILE = 1, NOUNLOAD, STATS = 10
GO
So what I would do is get the filenames from a certain folder and replace "db1.bak" etc. with the names I just picked from my folder and then run the script.
I know that there must be a smarter way, or is it maybe possible to "load" many .bak files into SSMS so the program will run them after each other?
in recent SQL version there is new Column "SnapshotUrl" so small modifications is required:
The following Powershell script worked best for me.
Source: Simon Osborne's SQL Blog
I had the same requirement and adapted the script found here, originally written by Chris Gallelli in 2003.
This version has been tested on SQL 2014.
Here is an example call to the Stored Procedure that does all the work:
The path obviously needs to be a path on the server that you are working against, not your own PC where you might be running SSMS from.
Here is the code for the Stored Procedure.
You will need to enable CMDSHELL in order to execute the SP. This script checks if you need to change anything:
Here is how to enable CMDSHELL:
If your file names match with your db names, you can do something like this :
Script of Collet with some adaptations worked for me. Be sure to enable xp_cmdshell before.
How to enable xp_cmdshell: