﻿{"id":252,"date":"2014-10-18T10:10:37","date_gmt":"2014-10-18T09:10:37","guid":{"rendered":"http:\/\/www.codereview.co\/?p=252"},"modified":"2014-10-15T14:55:36","modified_gmt":"2014-10-15T13:55:36","slug":"automatic-database-restore-using-datetime-functions-in-sql-server","status":"publish","type":"post","link":"http:\/\/www.codereview.co\/index.php\/sql-server-tutorials\/automatic-database-restore-using-datetime-functions-in-sql-server\/","title":{"rendered":"Automatic database restore using DateTime functions in SQL Server"},"content":{"rendered":"<p><span style=\"color: #000000;\"><strong>Scenario<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">I have to restore a production database to a development database on a weekly basis in order to refresh the environment. Currently, I&#8217;m doing this process manually. Is there any way to script this out so it can be automated?\u00a0 Check out this tip to learn more.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>\u00a0<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Solution<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">If you take full backups using SQL Server maintenance plans and let SQL Server use the default naming convention, you have probably noticed that usually you&#8217;ll have file name in the format of &#8220;database name + backup + date + time + .bak&#8221;. For example, a backup from the master database may look like this: &#8220;master_backup_2012_10_02_220558_8601773.bak&#8221;.\u00a0 It can be a challenge to script out automatic restores because the numbers on the end of the backup name constantly change. In this tip I will explain how to script out RESTORE DATABASE statements using DateTime functions.<\/span><\/p>\n<p><span style=\"color: #000000;\">Let&#8217;s say we have a folder full of backups like this:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-253\" src=\"http:\/\/i2.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/17.jpg?resize=300%2C230\" alt=\"1\" srcset=\"http:\/\/i2.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/17.jpg?resize=300%2C230 300w, http:\/\/www.codereview.co\/wp-content\/uploads\/2014\/10\/17.jpg 469w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/p>\n<p><span style=\"color: #000000;\">Let&#8217;s say our boss wants us to restore Monday&#8217;s production backup (Alabama) every Friday afternoon to our development database (Tide). To accomplish this task, we can use the built-in SQL Server DateTime functions.<\/span><\/p>\n<p><span style=\"color: #000000;\">The below script will restore from the backup created on the <strong>first day of the current week.<\/strong>\u00a0 I&#8217;ve added comments to explain the code.<\/span><\/p>\n<p><span style=\"color: #000000;\"><em>&#8212; Declare variables<br \/>\nDECLARE @backup nvarchar(200)<br \/>\nDECLARE @datebegin datetime<br \/>\nDECLARE @dateend datetime<\/em><\/span><\/p>\n<p>&#8212; Initalize variables<br \/>\n&#8212; Set @datebegin equal to the first day of the current week<br \/>\nSELECT @datebegin = DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)<br \/>\n&#8212; Set @dateend equal to the second day of the current week<br \/>\nSELECT @dateend = DATEADD(wk,DATEDIFF(wk,0,GETDATE()),1)<\/p>\n<p>&#8212; Set @backup equal to query dependent on datebegin and dateend<br \/>\nSELECT TOP 1 @backup = name + &#8216;.bak&#8217;<br \/>\nFROM msdb..backupset<br \/>\nWHERE database_name = &#8216;Alabama&#8217;<br \/>\nAND backup_start_date BETWEEN @datebegin AND @dateend<br \/>\nAND type = &#8216;D&#8217; &#8212; D is for full backups<br \/>\nORDER BY backup_start_date ASC<\/p>\n<p>USE [master]<\/p>\n<p>&#8212; Put DB in Single_User Mode<br \/>\nALTER DATABASE [Tide] SET SINGLE_USER WITH ROLLBACK IMMEDIATE<\/p>\n<p>&#8212; Restore DB using query from @backup variable<br \/>\nRESTORE DATABASE [Tide] FROM\u00a0 DISK = @backup WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5<br \/>\nGO<\/p>\n<p><span style=\"color: #000000;\">Below is a table of useful DateTime functions that you can use for the @datebegin and @dateend variables.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><strong>Day<\/strong><\/span><\/td>\n<td><span style=\"color: #000000;\"><strong>SQL<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Today<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT GETDATE()<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Yesterday<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(d, -1, GETDATE())<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">First Day of Current Week<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Last Day of the Current Week<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 6)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">First Day of the Current Month<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Last Day of the Current Month<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0)))<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">First Day of the Current Year<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">Last Day of the Current Year<\/span><\/td>\n<td><span style=\"color: #000000;\">SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)))<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">Another example may include where you need to take a backup from the first of the month of the production database and restore it weekly to the development database. In this situation you can edit the @datebegin and @dateend variables:<\/span><\/p>\n<p><span style=\"color: #000000;\"><em>&#8211;Set @datebegin equal to the first day of the current month<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>SELECT @datebegin = DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>\u00a0<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>&#8211;Set @dateend equal to the second day of the current month<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>SELECT @dateend = SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),1)<\/em><\/span><\/p>\n<p><span style=\"color: #000000;\">A quick and easy way to see if your DateTime functions are correct is to use the following:<\/span><\/p>\n<p><span style=\"color: #000000;\"><em>&#8211;Declare variables<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>DECLARE @backup nvarchar(200) <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>DECLARE @datebegin datetime<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>DECLARE @dateend datetime<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>\u00a0<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>&#8211;Set @datebegin equal to the first day of the current week<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>SELECT @datebegin = DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>\u00a0<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>&#8211;Set @dateend equal to the second day of the current week<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>SELECT @dateend = DATEADD(wk,DATEDIFF(wk,0,GETDATE()),1) <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>SELECT TOP 1 @backup = name + &#8216;.bak&#8217; <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>FROM msdb..backupset <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>WHERE database_name = &#8216;Alabama&#8217; <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>AND backup_start_date BETWEEN @datebegin AND @dateend <\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>ORDER BY backup_start_date ASC<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>\u00a0<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>PRINT @datebegin<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>PRINT @dateend<\/em><\/span><br \/>\n<span style=\"color: #000000;\"><em>PRINT @backup<\/em><\/span><br \/>\n<span style=\"color: #000000;\">After running this query you should return the dates and the backup name that will be used in the RESTORE statement:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-254\" src=\"http:\/\/i1.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/25.jpg?resize=288%2C73\" alt=\"2\" data-recalc-dims=\"1\" \/><\/p>\n<p><span style=\"color: #000000;\">From looking at the results returned I can determine that the @datebegin variable equals Monday, Oct 1, the @dateend variable equals Tuesday, October 2, and the backup that will be used for the restore is the backup created on Monday, Oct 1 at 10:15 PM.<\/span><\/p>\n<p><span style=\"color: #000000;\">\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Thanks for reading this article,<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Next steps :<\/strong><\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\"><strong>Share this with your colleagues because Sharing\u00a0 is Learning<\/strong><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Comment below if you need any assistance<\/strong><\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scenario I have to restore a production database to a development database on a weekly basis in order to refresh the environment. Currently, I&#8217;m doing this process manually. Is there any way to script this out so it can be automated?\u00a0 Check out this tip to learn more. \u00a0 Solution If you take full backups [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,3],"tags":[8,117,118,9,13,12],"_links":{"self":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/252"}],"collection":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":3,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":373,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions\/373"}],"wp:attachment":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}