Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Bespoke Software
    • Database Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft 365
      • Enabling the Multi Factor Authentication Application
      • Office 365 DNS Settings Generator
    • IT Hardware
    • Social Media Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Blocking Blank Senders
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Japanese
      • Translator English-Portuguese
      • Translator English-Spanish
      • Translator English-Turkish
    • Portfolio
    • Regulatory
    • Team
      • Chester Copperpot
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
عربى (AR)čeština (CS)Deutsch (DE)English (EN-GB)English (EN-US)Español (ES)فارسی (FA)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

A dynamic script to restore SQL Server database from a stored procedure

Create a centralised, generic and re-useable stored procedure using dynamic SQL script that can restore databases using a number of parameters as part of a wider maintenance plan

Process

Restore DB.png

The process of restoring a database can be scripted from SSMS. When this is saved into a Stored Procedure, it can be called from other processed or servers, and is particularly useful as part of a joined up maintenance plan.

Below we've created two scripts. 

With the simple script use the code generated from SSMS and replace the content of the Stored Procedure.

With the generic script, single databases could be restored using variables, meaning you could re-use it.

Simple Script

USE [utilities]GOCREATE PROC [maint].RestoreDatabase_{dbname} AS BEGINRESTORE DATABASE [dbname] FROM DISK=N'c:\backupfolder\{dbname}.bak' WITH FILE= 1,MOVE N'{dbname}' TO N'd:\database\{dbname}.mdf',MOVE N'{dbname}_log' TO N'e:\database\{dbname}.ldf',NOUNLOAD, REPLACE, STATS= 10,STANDBY=N'e:\database\ROLLBACK_UNDO_{dbname}.bak'ENDGO

Re-usable script

This script uses a number of parameters to enable it to be called from elsewhere, so can be re-used and called cross server if necessary.

  • @dbname - Database name that you want called
  • @Directory_Bak - Folder where the backups are stored
  • @Directory_Dat - It's best to keep log files and database files on different disks, so this is the directory you want to store it on
  • @Directory_Log - As above, use a separate directory for your log file
  • @Directory_Stand - This was developed with log shipping in mind, so this can be used to do a restore from your main database to the reporting database

SQL

USE [utilities]GOCREATE PROC [maint].RestoreDatabase(@dbname NVARCHAR(100),@Directory_Bak NVARCHAR(100),@Directory_Dat NVARCHAR(100),@Directory_Log NVARCHAR(100),@Directory_Stand BIT) AS BEGINDECLARE @SQL NVARCHAR(MAX)='RESTORE DATABASE ['+@dbname+'] FROM DISK=N'''+@Directory_Bak+''+@dbname+'.bak'' WITH FILE= 1,MOVE N'''+@dbname+''' TO N'''+@Directory_Dat+@dbname+'.mdf'',MOVE N'''+@dbname+'_log'' TO N'''+@Directory_Log+@dbname+'.ldf'',NOUNLOAD, REPLACE, STATS=10'--Standby CodeIF ISNULL(@Directory_Bak,'')<>''SET @SQL=@SQL+',STANDBY=N'''+@Directory_Stand+'ROLLBACK_UNDO_'+@dbname+'.bak'''EXEC master..sp_executesql @SQLENDGO

Author

Helpful?

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+442392064871info@claytabase.co.ukClaytabase Ltd, Unit 3d, Rink Road Industrial Estate, PO33 2LT, United Kingdom

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader