How Do I Measure My DBA Skills Part 7

Over the past few weeks I have been writing about DBA skills for various career levels and, so far, for a couple of different DBA types – the Production DBA and the Development DBA. Today, we’re examining the Lead Development DBA. You’ll discover what this person needs to know

DEV DBA III/Lead Dev DBA – 5 to 7 Years

  1. All competencies from previous level.
  2. Manages most administrative aspects of non-prod environments without assistance from other DBAs.
  3. Participate in T-SQL development and database design of advanced complexity.
  4. Investigates potentially complex data issues within SQL Server.
  5. May have several years of progressive experience with SSIS, SSRS or SSAS.
  6. Administers source control systems for the environment.
  7. Regularly automates tasks. This should include the use of T-SQL, SQL Server Agent, and PowerShell.  May include the use of other languages like Python or C#.
  8. Creates and manage design documentation related to development work.
  9. Regularly explores, learns and implements new SQL Server development features. Examples would include new T-SQL enhancements and new feature sets like Temporal Tables for SQL Server 2016 and Data Virtualization for SQL Serer 2019..
  10. Advanced understanding of execution plans, indexes, and query tuning.
  11. Investigate complex data integrity/repair issues within SQL Server.
  12. Leads in knowledge sharing in two or more areas from levels I, II, or III.
  13. Assists with SQL Server migrations and upgrades in DEV/QA or production.
  14. Mentors Junior DBA’s and Developers.
  15. Participates in code reviews.
  16. May contain competencies from Production DBA levels up to level 1 or 2.
  17. Demonstrates emotional intelligence and may take an interest in leadership roles.

Managing Non-Prod SQL Servers

As a Lead Development DBA, you are managing your company’s non-prod SQL Servers without the regular input of other DBAs.. That doesn’t mean you know everything about managing the environment, but it does mean you are no longer asking any basic questions. You’re managing new SQL Server installations and post-install configuration, managing access to the SQL Servers, and SQL Server maintenance (backups/restores, index and statistics maintenance, CHECK DB, etc).

You Have T-SQL Skills

You know how to use views for inserting, updating or deleting data. You know when to use a temporary table, a table variable or a derived table. You also know when dynamic SQL is appropriate, and no the answer isn’t “never.” Your stored procedures have error handling using TRY… CATCH and/or THROW and they do data validation for inputs. You know and avoid the problems associated with scalar user defined functions. You consume development material from people like Itzik Ben Gan and Gail Shaw without your eyes glazing over.

SQL Server Performance Tuning and Optimization

Performance Tuning and optimization is your bread and butter. You’ve probably read the various editions of books on execution plans by Grant Fritchey. You know who Brent Ozar and Hugo Kornelis are and you have frequented their websites to learn about execution plans and making SQL Server go fast. You know how to leverage the Query Store. MAXDOP and Cost Threshold of Parallelism are things you understand and know what to do with at the server level and how it affects queries running on the SQL Server. You also know that MAXDOP can be set at the database level in newer versions of SQL Server. If you didn’t know that, see this.

Working With Other SQL Server Components and Features

Depending on the needs of your employers, you are likely fairly comfortable with SSIS, SSRS or SSAS. You can develop solutions with those tools that go beyond basic things. SSRS Administration using the Report Manager and Report Server URLs is familiar to you, for example. Complex SSIS packages are something you’re not afraid of at this point.

Mentoring and Knowledge Sharing

At this stage of your career you will be expected to be a leader in your group and share knowledge with others. You will be skilled enough to share your growing expertise in 2 or more areas. You may be the go to person for execution plan analysis, Query Store and T-SQL.  Share that knowledge with others. Make someone else’s professional life better by sharing your experience. This will not only benefit the other person, but you as well.  Nothing solidifies learning like trying to teach something to someone else!

As part of your leadership among other people, you should demonstrate a fair amount of emotional intelligence. You are able to manage your own emotions and the emotions of others during your interactions. You’re also taking on more and ore the role of a leader at this phase of your career. People will be looking to you for guidance and decision making.

Next Steps To Take

  1. Look at the criteria in this post and make a training plan for the things you know you need to work on and put that plan somewhere that you will see on a regular basis.
  2. Contact me here for questions about this post or the skills listed in it. You can also reach out to me via Twitter using the handle @leemarkum.

 

How Do I Measure My DBA Skills Part 6

With the last post, we began talking about the Development DBA and the skills needed at the beginning of this career path. Now it’s time for you to ponder what the next level looks like.

Dev DBA II – 2 to 4 Years of Experience

  1. All competencies from level 1
  2. Develop basic to intermediately complex stored procedures, triggers, views and other database objects.
  3. Will have some experience with SSRS, SSIS, and/or SSAS development, deployment and possibly administration.
  4. May administer source control systems for the environment.
  5. Promotes SQL changes from Dev up through all environments, including production.
  6. Contributes to automation, particularly using native SQL Server methods, but could also leverage PowerShell or other languages for automating tasks (Python, C#, etc.)
  7. Designs pre-prod testing of SQL code prior to production upgrades or migrations. This includes performance and regression testing.
  8. Basic to mid-level understanding of execution plans, indexes, SQL Server statistics, and query tuning.
  9. Investigate basic to moderately complex data integrity/repair issues within SQL Server
  10. May begin leading in knowledge sharing in some capacity in one or more areas from level I or II.
  11. May take an active interest in leadership and in development of leadership skills, including emotional intelligence.

In years 2 through 4 the Dev DBA will work with more complex T-SQL such as recursive CTE’s, and error handling with TRY CATCH or the THROW syntax will likely be introduced at this stage. You may begin writing database views during this part of your career and leveraging them to make query writing a bit simpler for yourself and others. You are likely to be exposed to T-SQL Triggers and you’ll need to understand them and even maybe write a few triggers of your own.

You will likely start to get some sort of regular exposure to SSRS, SSIS or SSAS at this point. This will of course be based partly on your own interest in learning the tools of the trade and what is in use already at your employer.

Source control and the process of promoting changes from source control up into QA, and even production, may be something that begins at this level. After all, as a person who is writing a fair amount of code, you will have an interest in ways to manage code changes.

Automation

 

automation example

Let me ask you a question. Would you rather wash clothes manually with a washboard and tub or use a washing machine? It seems like a silly question. You would want to use a washing machine. You probably can’t imagine washing clothes without one.

In a similar thought process, there are things that you and others at your job will not want to do over and over in a manual way.  Accordingly, you will make more significant contributions to automation of tasks at this level in your career. SQL Agent will become your friend and learning to script out processes to make them easily repeatable, rather than doing everything in the SSMS GUI by hand, will occur more frequently. You may automate tasks with PowerShell for sure, especially using DBATools, but Python may also be an option. In case you didn’t know, Python does integrate with SQL Server via a specific driver.

An important part of the Dev DBA’s work is testing SQL code. You’re going to be doing more of that at this level in your career. You will need to verify query results and probably automate repeated testing of code as well so that you can test not only results but performance. Maybe the query returned the results you expected but it took too long to do it. This is where learning how to read execution plans will come in.  Execution plan operators will give you clues about what is happening with your T-SQL and those clues can help you with re-writes or with indexing.

Aaah yes. Indexes.  You’ll need to begin understanding indexes as well.  You’ll need to know what they are, the different types of indexes and when to use each type. There are clustered indexes. Indexes made as a result of creating a primary key. These may be clustered or non-clustered primary keys. There are non-clustered indexes. There are filtered indexes. There are non-clustered columnstore indexes. There are clustered columnstore indexes.  As a corollary, you’ll need to look into the concept of SQL Server statistics as well.

Next Steps to Take

  1. If you’ve read the other parts of this series, then you know I’m going to tell you to copy the skills list above to a Word doc and place an “X” next to the things you need to work on.
  2. Use those “X” marks to build a training plan. Put the training plan somewhere that you will see every day. One thing that should be on your training plan at this stage is learning more T-SQL.
  3. Work the plan. I have a post that will help you find resources for your learning plan.
  4. Contact me here for questions about this post or the skills listed in it. You can also reach out to me via Twitter using the handle @leemarkum.

How Do I Measure My DBA Skills Part 5

We’ve been on a journey recently in this series as we discuss measuring your DBA skills. Previous posts have been intriguing to you. You like where this is going and yet, your skills are different from what we’ve been discussing in this initial part of the series. Maybe you are a Developer who works with databases and you find SQL Server very interesting. You build some tables and write some queries as part of your work and you really like doing it. You like trying to figure out how to make your initial draft query for your project run faster. You have conversations about this with other Developers or maybe even a DBA or two at your company. What you hear resonates with you and deepens the interest in SQL Server. You wonder, “Is there a place for me in the SQL Server world? What would I do if I did a career pivot to work with SQL Server almost exclusively?” Enter the Development Database Administrator.

 

What Does a Development DBA Do?

Before we jump into a discussion of skills, we need to try to answer the question, “What does a Development DBA do?” After all, if you think you’re interested in this, what are you signing up for?

A Development DBA will often be responsible for the management of the non-prod SQL Servers, SQL development work for applications and processes, task automation and perhaps source control and the change deployment process. Non-prod SQL environments need care and maintenance. The Dev DBA role can assume this responsibility. This isn’t because the Dev DBA is a “Junior” role, but because this non-prod environment plays the critical role of testing grounds for development work. This non-prod environment will be where the Dev DBA works and experiments with new SQL Server development such as stored procedures, SSIS work SQL Agent job development etc., often in tandem with Developers from other areas of the company, to produce new products and applications. The Dev DBA spends most of their time in non-prod so there is familiarity and as a result it makes sense to have the Dev DBA manage the environment.

Related to the experimental nature of the work, and the need to promote that work to production, the Dev DBA will often participate in or wholly manage source control and change processes such as source control software and the design of the process of promoting code through the various developmental levels of the environment. Once deployments are tested, scripts are then often handed off to Production DBAs for deployment of new code in production, but even this could be automated by the Dev DBA with control over when the deployment happens being wielded by the Production DBA.

At higher levels, the Dev DBA role may begin to overlap some with the Production DBA role and skillset. Let’s dive into the skills of a Development DBA.

The Development DBA Role

DEV DBA – 0 to 2 Years Experience

  1. Design tables utilizing basic normalization techniques.
  2. Basic T-SQL development. SELECT, INSERT, UPDATE, DELETE. Stored procedure development with guidance from more experienced DBAs.
  3. Manages most administrative aspects of non-prod environments with assistance from more experienced DBAs, including the application of patches to pre-production.
  4. May participate in pre-prod testing of SQL code prior to production upgrades or migrations.
  5. May have some exposure to SSIS, SSAS or SSRS.
  6. Demonstrates understanding of basic backup/restore processes.
  7. Demonstrates values driven behaviors such as humility, integrity, teamwork and is teachable.

T-SQL Skills

Now that you’ve looked over the list, let’s talk about it. T-SQL skills and overall development skills are at the heart of this role. The Dev DBA will spending a lot of time manipulating data and the first and foundational way that will happen will be with the core skills of SELECT, INSERT, UPDATE and DELETE. Introductory and basic T-SQL skills will be learned, practiced and built upon as you spend time in this role. A corollary to that will be learning all about the relational database table. At this phase of the career the Dev DBA will be learning what a good relational table should look like. Even if you can’t quote them, the principles of first, second and third normal form will become familiar as you design more and more tables.

Stored procedure development will be learned in this part of your career. You’ll be exposed to what a stored procedure is, why it should be used, advantages to it, the basic form of a stored procedure and so on. Of course, you’ll begin to write some of your own stored procedures as well. After all, there’s no point in learning about them, but not actually writing them!

In this phase of your career you maybe be asked to make some logins and users as part of your development work. You’ll need to learn what those database objects are, how they are different and how they work together to provide authentication and permissions. You will, I hope, be doing this with the help of other DBAs who have walked this path before you so that you’re not scratching and clawing to figure it out on your own. My point is, other people will be guiding your work at this phase.

You will be testing code, a lot. After all, in this role you are creating things in non-prod that have to be reliable. You will be making sure that result sets make sense.

Depending upon the technologies in use at your company, you may be exposed to things beyond the T-SQL and basic security that we’ve already mentioned. SQL Server Integration Services (SSIS) may be  important at your work, and as a result, you will need to have some basic understanding of it. Maybe your employer has invested heavily in SQL Server Reporting Services (SSRS). In which case, you will be exposed to report development and deployment.

What Happens When I Lose Some Data?!

At this point in your career, you probably aren’t taking backups or restoring backups. But you should begin learning backup and restore concepts. You’re going to need that understanding as your career grows. Trust me!

You are going to make a mistake in a non-prod environment and wipe out a small, or large, amount of data. Please accept that this is going to happen. You’re going to make a mistake. You’re going to freak out and think, “I’m going to be fired!” I hope that you won’t be fired over it, especially since you’re not working in production. If you introduce me to a person who says that they’ve never messed up or lost any data, I will have met either a liar or a person who hasn’t worked with data nearly long enough!

When this happens, and it will happen, relax, and ask for help. If the other people on the DBA team have done their first and most important job, you’ll be able to get the data back. If they haven’t done their first and most important job, then maybe they are the ones who should be worried!

Do I Really Need People Skills?

Just like the other posts in the series, the last thing on the list is about people skills. Yes, you’re going to need them.  You have to interact with other people on your team, people on other teams and maybe even external customers. You need to be teachable, develop good listening skills and be willing to help other people. All of these things will help you lean your craft. You need the other people around you because they know things and have experience that you need. Remember that example above where you’ve deleted data you need to get back? If you don’t work on your people skills and have good relationships with co-workers, then that conversation where you have to ask for help is going to be reeeaaallllly awkward!

Particularly at this level of your career, other people can help you avoid pitfalls in your code as well as help you get your next promotion. So, be humble and willing to ask questions. When someone answers your question, don’t argue. If you don’t like the answer or you think the person is wrong, ask more questions to get clarification. Say something like, “I’m not sure I follow. Can you try explaining that another way?” Or you might say, “I hear what you’re saying. Would there be anything wrong with doing it like ‘X’? Is there a reason that wouldn’t work?”

Next Steps To Take

  1. Copy/paste the skills list into a Word doc and place an “X” next to anything you need to work on.
  2. Pick two or three things to focus on and build yourself a training plan. Put that training plan somewhere that you will see it every day.
  3. Ask a person on your team how to do a task that is important in your environment, but that you don’t know anything about yet.
  4. Talk to me. Contact me here if you would like specific help with anything in this post or other things related to SQL Server. If there is an issue with the form, you can reach me at leem@leemarkum.com. I will be glad to help.

How Do I Measure My DBA Skills Part 3

In this series on measuring your DBA skills, we’ve been discussing what skills you should have at each phase of your career.  So far we’ve covered the entry level skills and the phase 2 skills a Production DBA needs. In this part of the series you’re discovering what a Production DBA needs to know or be able to do at each stop along the career journey.  If you want career development, you need to work on these skills in order to move forward.

So what’s next after that second phase of career development for a Production DBA?  Once you’re into your career about 5 or so years, what should you be reaching toward?  I’m glad you asked! Let’s take a look at the skills in the next phase!

Production DBA III/Lead DBA: 5 – 7 years of Experience

  1. All competencies from the previous level.
  2. Designs backup/restore strategy.
  3. May participate in T-SQL development and database design of advanced complexity.
  4. Investigates potentially complex issues within SQL Server using native approaches, and when available and appropriate, 3rd party software, such as monitoring software.  Deploys changes based on that investigation.  These could be performance issues at the server level or query level.
  5. Implements high availability/DR solutions, often in tandem with infrastructure teams and may participate in the design of HA and DR solutions.
  6. Manages more complex troubleshooting scenarios for high availability and disaster recovery technologies (DB mirroring, replication, log shipping, potentially AlwaysOn Availability Groups or Failover Cluster Instances)
  7. Interacts with Systems Administrators or Infrastructure teams in matters of hardware, virtualization and storage, primarily concerning performance investigation.
  8. Regularly automates tasks using T-SQL and PowerShell.
  9. Demonstrates a basic proficiency with Extended Events.
  10. May lead SQL Server migration planning and implementation.
  11. Leads knowledge sharing in three or more areas from levels I, II, or III.
  12. Mentors Junior DBA’s.
  13. May possess competencies from the DEV DBA career path up to level 2.
  14. Demonstrates good self-awareness, self-management, social awareness and relationship management skills (emotional intelligence).

Working Independently

At this level in your career as a Production DBA your work is done in a fairly independent manner. You are capable of handling all the things from levels 1 and 2 and you’re continuing to advance your skill progression on things like design decisions for SQL Server. You’re doing more designing for things like backup/restore strategy, high availability, as well as participating in the planning of database and server migrations.

Adding More Advanced Skills

At this level you’re also adding skills with technologies like Extended Events and Query Store for troubleshooting and monitoring as well as learning how to use 3rd party software to find and troubleshoot issues in the environment.  You’re getting hands on experience with more advanced troubleshooting scenarios such as problems related to AlwaysOn Availability Group or Failover Cluster Instances, log shipping, etc.

Typically, you’re doing less and less entry level work, like troubleshooting and resolving problems like SQL Agent job failures. This could be in part because you’ve been at the company for awhile and you yourself have stabilized performance for things like SQL Agent jobs or you’re working at a place that has been taken care of by another DBA in a fairly good, consistent way.  Also, depending on the size of your environment and your employer you probably aren’t the only DBA at the company at this point so you may have teammates who carry at least some of the workload for phase 1 career tasks.

Sharing Knowledge as a Database Administrator

Knowledge sharing becomes more prominent in this part of your career.  This teaching aspect of your skills will be across several areas of SQL Server data management based on topics and skills in phases 1, 2, or 3. Perhaps you’re directly mentoring Junior DBAs on the team.  You may be giving presentations at work or maybe even your local SQL Server Users Group or a SQL Saturday on things like backup/restore functionality and design, how to automate various tasks, or maybe on something like Extended Events.

You’ll notice that there is some mention of development skills in this skills list.  Now, production DBAs typically don’t do much development. However, I realize that some employers don’t have a clean break between Production and Development responsibilities for their DBAs. Sometimes an employer needs or wants an overlap of skills. I also acknowledge that your own interests might mean that you decide to work on some development tasks like writing stored procedures for some application work that is being done or maybe working on table and database design considerations.

This begins to bleed over into a category of DBA that we will cover later in the series called the Dev DBA.  Again, your work environment may dictate the need for these skills or you may have an interest in being competent with some entry level Dev DBA skills. Either way, those skills may begin to show up in your career at this level.

Next Steps to Take

  1. Copy/paste the above numbered list to a Word doc.  Think carefully about each one and whether or not you currently meet this criteria.  Put an “X” next to any item you need to work on.
  2. For each item you placed an “X” next to, create a plan for improving skills related to that item. Are you going to watch Pluralsight videos, find resources on YouTube or read blogs about the topic?  Make some decisions and a plan about your approach. Also, at this career level and higher, you need to give yourself 30-60 days for each new skill you want to learn. You won’t be working on this skill for an actual 30 days, but with everything else you’re doing in your day job, it may take you a month or more to accumulate enough time with the skill for you to gain competency.
  3. If you would like help with anything in this post, or with something else related to SQL Server, reach out to me here  or on Twitter, and I’ll be glad to offer assistance.

 

3 Ways to Restore SQL Server Databases

This is likely the last in a series of three posts about backups and restores.  My aim with this series has been to give you an idea of the options available and a basic start on using those options. I’m not trying to go in-depth on the subject of backups and restores.

Today will be about getting you going on the concept of restoring databases in SQL Server.  I’m using a local install of SQL Server 2017 on my desktop PC to demonstrate these techniques.

Obtaining the StackOverflow Database

Before I dive into demonstrating the restore options, I want to talk a little bit about the set up. I’m using the StackOverflow2010 database.  Anonymized data dumps of the StackOverflow database are available here. This particular version of the database I’m using came from a page on Brent Ozar’s site.

I then used the below PowerShell code to take backups.  Between the Full and Diff backup I added one user to the Users table.  Between the Diff and Log backup I add two more users.

backup-dbadatabase -SqlInstance MySQLServer2017InstanceName -Database 'StackOverflow2010' -Path 'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010' -Type Full -FilePath dbname-backuptype-timestamp.bak -ReplaceInName -CompressBackup -Verify

backup-dbadatabase -SqlInstance MySQLServer2017InstanceName -Database 'StackOverflow2010' -Path 'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010\' -Type DIFF -FilePath dbname-backuptype-timestamp.bak -ReplaceInName -CompressBackup -Verify

backup-dbadatabase -SqlInstance MySQLServer2017InstanceName -Database 'StackOverflow2010' -Path 'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010' -Type Log -FilePath dbname-backuptype-timestamp.trn -ReplaceInName -CompressBackup -Verify

Below is the T-SQL I used to insert some rows.  That value for Age though!  Ah, to be 25 again!

 

INSERT INTO dbo.Users ([AboutMe], [Age], [CreationDate], [DisplayName], [DownVotes], [EmailHash], [LastAccessDate], [Location], [Reputation], [UpVotes], [Views], [WebsiteUrl], [AccountId])
VALUES
('I make SQL Server more stable, highly available and easier to manage', 25, GETDATE()-7, 'Lee Markum', 0, NULL, GETDATE(), 'Saint Louis, MO', 0, 1000000, 2000000, 'LeeMarkum.com', NULL)

--Took a differential backup here so I would have some changes to restore

INSERT INTO dbo.Users ([AboutMe], [Age], [CreationDate], [DisplayName], [DownVotes], [EmailHash], [LastAccessDate], [Location], [Reputation], [UpVotes], [Views], [WebsiteUrl], [AccountId])
VALUES
('I make T-SQL solutions that just work', 25, GETDATE()-7, 'T-SQLGuru', 0, NULL, GETDATE(), 'Saint Louis, MO', 0, 1000000, 2000000, NULL, NULL),
('I make SSIS solutions that just work', 25, GETDATE()-7, 'SSISGuru', 0, NULL, GETDATE(), 'Saint Louis, MO', 0, 1000000, 2000000, NULL, NULL)

--Took a log backup here

Using SSMS to Restore a Database

Just like for creating a backup, SQL Server Management Studio has menu options for restoring a database. Open your copy of SQL Server Management Studio and connect to a non-production or lab environment of some kind. You’re going to right click the database you want to restore and use the fly out menu to navigate to Tasks > Restore > Database.

 

Selecting the Database option from the fly-out menu above brings you to the below page. Notice in the following screen that SQL Server finds the applicable backup files for you. No in-depth knowledge of restore operations is needed in this case because the work is done for you. It has found the full, differential and log backups that I took earlier.  Each row represents a backup file.

If you only want to restore to the time represented by the differential backup, then uncheck the box next to the log backup.  SQL Server will then restore the full and differential backup, but not the log backup. You might want to do something like this if you know that the data in the database was corrupted or changed in an unexpected or undesired way immediately after the differential backup.  As a result, you restore the database to the point in time represented by the differential in order to restore the database to its last known good condition.

In the Destination section, SSMS fills in the name of the database that you selected earlier for restoring.  However, if you give the database a different name here, then you can restore the database and compare the restored database to the current database.  You might want to do this in order to attempt to repair data in the current database by getting data from the restore ddatabase. Please note the yellow banner that informs you that this process will take a tail log backup.  This is a preventative measure.  It captures the last portion of the transaction log called the tail of the log.  It allows for complete recovery.  Without this backup, if you restore this database, you could lose transactions.

 

One other option I want to point out on this screen is what the “Timeline” button is for. When clicked, you’ll arrive at a screen like the below.  This allows you to choose a point in time to restore to based on the available backups.  This point in time restore is only available for databases in the full and bulk-logged recovery model. For bulk-logged recovery model, certain rules apply as to when this option would be available.

If you select the radio button next to “Specific date and time” then you can either specify a certain date and time in the Date and Time boxes or you can also use the slider icon along the bottom.  There is also a Time Interval drop down that controls what you see in the colored timeline above the slider icon. Using this option is the same as using the WITH STOPAT command using T-SQL for the restore. For my demo, I have not used this option, but I want you to be aware that it is available. This Timeline feature is new in SQL Server 2012.

 

Once you have the General page looking the way you need it, click on the Files selection on the left to bring you to the page below. Here you have one option to work with and that is whether you want to relocate the database files to another folder. Checking this option allows you not only to relocate the files, but rename them as well.  You might want to do that if you’re restoring the database to a different name so that you don’t have two databases with different names, but the same file names. That might make it more difficult later to figure out which files belong to which database without running a query to figure it out.

Should You Use the WITH REPLACE Option?

Now click on the Options selection on the left.  You arrive at the below page. The option “overwrite the existing database (WITH REPLACE)” will bypass certain checks that would normally occur during the restore process.  As a result, you really would rarely use this option, if ever. This option will do what it sounds like and replace the target database with the database in the backup file.  If you mistakenly point this restore to database on a different server that isn’t the same database that is in the backup and use this WITH REPLACE option, then you will overwrite the other database on the other server. There is a large section in this page about caution using the WITH REPLACE option.

https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-2017

Here is where the option for taking the Tail-log backup is located.  You should leave this checked and note the location for where that backup will be written.  If it isn’t the location you would like, change it in the Backup file box.

There is also an important option under “Server connections.” If you leave this unchecked and there are current connections to the database then the restore will fail.  A restore operation needs exclusive access to the database in order to perform a restore so other connections must be terminated first.  When this option is selected, SSMS will take care of that for you.

Once all the options you want are filled in, I would encourage you to click the Script option at the top so you can obtain the T-SQL equivalent for what SSMS is going to do. You can either click OK in the lower right of SSMS to execute the restore, or click Cancel and go to the T-SQL to review what will be done and then execute the T-SQL.

 

Now let’s turn our attention to using T-SQL for restoring a database.

Using T-SQL to Restore a Database

Below is the generated T-SQL for restoring the StackOverflow2010 database based on the available backup chain on my local SQL Server. I have added comments so you can better understand what each part is doing.  T-SQL syntax for performing a restore is, in most scenarios, not too difficult.  I am not showing things like file and filegroup restores or piecemeal restores.  These are more advanced scenarios.

USE [master]
/*This is the first part of the T-SQL generated when the "close existing connections" option is chosen in the GUI. This gives the session doing the restore the exclusive access needed.
*/
ALTER DATABASE [StackOverflow2010] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

--This is the tail-log backup
BACKUP LOG [StackOverflow2010] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010_LogBackup_2020-10-04_10-23-33.bak' WITH NOFORMAT, NOINIT,  NAME = N'StackOverflow2010_LogBackup_2020-10-04_10-23-33', NOSKIP, NOREWIND, NOUNLOAD,  NORECOVERY ,  STATS = 5

/*Here is the full backup.  Database restores need a place to start from and the full backup is that place.
*/
RESTORE DATABASE [StackOverflow2010] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010\StackOverflow2010-Full-202010030919.bak' WITH  FILE = 1,  NORECOVERY,  NOUNLOAD,  STATS = 5

--Here is the differential backup
RESTORE DATABASE [StackOverflow2010] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010\StackOverflow2010-Differential-202010030938.bak' WITH  FILE = 1,  NORECOVERY,  NOUNLOAD,  STATS = 5

--Her are the two log backups to restore
RESTORE LOG [StackOverflow2010] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010\StackOverflow2010-Log-202010030947.trn' WITH  FILE = 1,  NORECOVERY,  NOUNLOAD,  STATS = 5
RESTORE LOG [StackOverflow2010] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010_LogBackup_2020-10-04_09-20-23.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5

/*This is the second part of the T-SQL generated when the "close existing connections" option is chosen in the GUI. This puts the database back to multi_user so it is ready to be used*/
ALTER DATABASE [StackOverflow2010] SET MULTI_USER

GO

 

Now let’s take a look at doing this with PowerShell

Using PowerShell to Restore a SQL Server Database

There are commands in the PowerShell on your local PC or server that will perform restores of SQL Server backups.  However, we will be discussing the PowerShell module called DBATools, which has to be installed before it can be used.

Please go to https://dbatools.io/download/ to see how to obtain and set up the module for use with this part of the blog post. I won’t be covering that here as the material is well documented on their site.

Once you have that, open PowerShell as an Administrator.  Type in the below code and hit F5.

Help Restore-dbadatabase -Detailed

This will give you a full description of the parameters and switches available in this command.  Numerous example code snippets will also be shown. From that help information I wrote the two examples that follow.  The first example will not perform the restore but instead writes to a .sql all the T-SQL needed to perform the command.  Strictly speaking, the -DatabaseName and value aren’t needed because the only backup files in that directory are for the StackOverflow2010 database. This option is useful so you can see what the command is going to do.

In the second example, the PowerShell will actually do the restore and then write it’s actions out to a text file. This is useful for having a history of what the command did.

In the third example, this will  use the folder structure from Ola Hallengren’s world famous maintenance solution. Notice the following switch:

-MaintenanceSolutionBackup

There are also switches for maxtransfersize, buffercount, and blocksize.  These can all be experimented with for your databases to find an optimal combination that restores the database as fast as possible. Additionally, there is a -RestoreTime option that is the equivalent of the WITH STOPAT syntax in T-SQL and the TimeLine function in the SSMS GUI.

<#This will not perform the restore, but will instead create a .sql file of the necessary T-SQL to do the requested restore.
#>
Restore-DbaDatabase -SqlInstance MYPC\Kronos2017 -DatabaseName StackOverflow2010 -Path 'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010' -WithReplace -OutputScriptOnly | Out-File 'C:\DBAToolsRestoreScripts\RestoreStackOverFlow2010.sql' 

<#This will do the restore and create a text file showing what the PowerShell command actually did
#>
Restore-DbaDatabase -SqlInstance MYPC\Kronos2017 -DatabaseName StackOverflow2010 -Path 'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010' -WithReplace | Out-File 'C:\DBAToolsRestoreScripts\RestoreActionsStackOverFlow2010.txt' 

<# This will do the restore based on Ola Hallengren's directory structure from his world famous maintenance solution.  Notice the switch -MaintenanceSolutionBackup
#>
Restore-DbaDatabase -SqlInstance MYPC\Kronos2017 -Path 'C:\Program Files\Microsoft SQL Server\MSSQL14.KRONOS2017\MSSQL\Backup\StackOverflow2010' -MaintenanceSolutionBackup -WithReplace -OutputScriptOnly | Out-File 'C:\DBAToolsRestoreScripts\RestoreStackOverFlow2010.sql' 

Next Steps to Take

  1. Download and install DBATools.  It’s useful for a lot more than handling SQL Server restore scenarios!
  2. If you’re not familiar with PowerShell, you need to learn it, if for no other reason than the options that DBATools provides you for working with SQL Server.
  3. Connect to a test server and try out each method. Get some experience with these methods so you know which one you like the most and so you’re not trying to figure out how you want to perform a restore when you actually have to do it for real.
  4. If you would like help with anything in this post, or with something else related to SQL Server, reach out to me here, or on Twitter, and I’ll be glad to offer assistance.