07/03/2014
by Marlon Ribunal
0 comments

Executing scripts on multiple servers the easy way

Central Management Server and server groups provide a convenient way to manage multiple servers and databases in one place and at the same time.You can register and manage any servers but only those in SQL Server versions 2008 and higher can be designated as Central Management Server. You can execute TSQL scripts on any or all the servers registered under the Central Management Server at the same time. If you have a Policy-Based Management structure in place, the Central Management Server makes it easy to execute policies on multiple servers at once. I’ve said that enough – at the same time. Click once, and you’re done with all the servers.

But what if, for some reasons, you cannot use Central Management in your environment?

This is the reason why tools like SQL Multi Script from Red Gate Software exist. Some of the features I like are:

  • Set the execution order of multiple scripts
  • Query databases in parallel
  • Messages from SQL Server displayed for all databases
  • Execute just the selected text in a script (just like in SSMS)

Let’s take a look. For this demo, we’re using two SQL Server 2012 instances from two separate servers on a domain. This demonstrates that we can use the SQL Multi Script tool on multiple servers across a network.

The first time you run the tool, you need to build your Database Distribution List:

1. Click the Configure button Build Database Distribution List 2. On the Configure Database Distribution List Dialog, click the New button. Type in a name for the new distribution list, then click Create. Let’s put SQL2012_Dev_Servers for this demo. This list is for databases, but I’d still like that “servers” in the nomenclature to have that logical grouping by servers. Using distribution lists is a good way of having that separation among server groups; e.g., Test, Dev, QA, Production, etc. So name your lists as intuitive as possible. Creating New Distribution List in SQL Multi Script 3. Still on the Configure Database Distribution List Dialog, click the Add a SQL Server Not Listed button. Type in the SQL Server whose databases you want to add to the distribution list, specify your authentication credentials, then click Add. Remember the distribution list is for databases, not for servers. Repeat this step for all the servers you want to add. Add SQL Server to Multi Script Distribution List 4. Now that we have added the servers, let’s add the databases we want to list in our SQL2012_Dev_Servers distribution list. Select the databases you want to add in the distribution list. You can multi-select databases among the servers by pressing CTRL + Right Click key. Then, click the Add button. Select database to add to the multi script distribution list 5. You can now see the databases in the Databases to Execute Against pane under the Distribution list. Click OK. Add database to databases to execute against in SQL Multi Script 6. You can create a new script via the SQL Multi Script editor; and, save that script for later use. You can also add an existing script. For this demo, let’s do the latter. Click the Add button. Select the script you want to add from the Add File Dialog box. I’m adding two scripts for this demo: a script that creates a database called DBTools and sp_Blitz by Brent Ozar Unlimited. I don’t intend to violate the sp_Blitz trademark here. I just want to demo that an enterprise-ready script such as sp_Blitz can execute in SQL Script.

add existing scripts to SQL Multi Script

 7. Like I mentioned in the beginning of this post, one of the features that I really like in SQL Multi Script is the ability to set the execution order of multiple scripts. The first script, Create_Database_DBTools.sql, creates a database called DBTools. The second script, the sp_Blitz (Brent Ozar Unlimited trademark), creates the sp_blitz stored procedure on the DBTools. Let’s order the two scripts so that the Create_Database_DBTools runs first before the sp_blitz script; otherwise, it will throw a missing object error. I modified the sp_blitz to run on the DBTools database instead of the default database (master).

Let’s parse the script to make sure our scripts are error-free. In this demo, I’m getting a “DBTools database does not exist” error, which makes sense, because the DBTools database has not been created at this point.

One of the features of SQL Multi Script is its ability to display Messages from SQL Server, which exactly is what we see here:

SQL Server message displayed on SQL Multi Script

8. If you’re executing long and complicated scripts, encountering errors is a possibility. SQL Multi Script provides a list of actions to be taken when an error occur; i.e., Continue Executing, Skip script on database with error, Stop executing on database with error, and Stop executing.

On error action when script has error on SQL Multi script

 

9. Let’s execute our scripts. We should get a confirmation that the scripts ran successfully. The tools did not encounter an error this time because we executed the two scripts in the correct order.

Script run successfully confirmation on SQL Multi Script

10. Let’s check our databases through SSMS, and make sure that the objects we just created through SQL Multi Scripts exist. The following screen captures show that the DBTools database and sp_blitz stored procedure objects are indeed created on both the SQL2012-A and SQL2012-B servers.

objects created by SQL Multi Script

database objects created by SQL multi Script

11. But here’s more. Here’s another feature I like in the tool – it’s ability to display the results of the script. Let’s execute the sp_Blitz via the SQL Multi Script tool. But before we do that, let’s add the DBTools from both SQL2012-A and SQL2012-B to our SQL2012_Dev_Servers distribution list. This time, instead of adding an existing script, let’s create a new script in the editor:


EXEC [DBTools].[dbo].[sp_Blitz]
@CheckUserDatabaseObjects = 1 ,
@CheckProcedureCache = 0 ,
@OutputType = 'TABLE' ,
@OutputProcedureCache = 0 ,
@CheckProcedureCacheFilter = NULL,
@CheckServerInfo = 1

Let’s execute that TSQL. SQL Multi Script should be able to show us the results of the sp_Blitz stored procedure, like this:

SQL Multi Script Query Results

The SQL Multi Script is a neat tool. I recommend it if you’re looking for something like this.

05/23/2014
by Marlon Ribunal
0 comments

Failed To Lock Virtual Machine’s Configuration

[Updated]

I rebuilt my Hyper-V virtual machines / virtual network by deleting the old ones and building new machines on a portable external hard drive. So after a while of not starting up the host laptop, I could not start any of the machines. First, I noticed that the state of the machines on the Hyper-V Manager now says “Off-critical.” I am getting this connection error: Failed to lock virtual machine’s configuration. Unspecified error (0x80004005). Hyper-V Virtual Machine State Error   I checked the Hyper-V Setting and I found out that the folder path of the Hard Disks and Virtual Machines files has been changed to its default setting. I’m not sure why but that’s exactly what happened:  (This has nothing to do with this error. See correction at the bottom of this post.) Hyper-V Connection setting path changed to default path Needless to say, I just needed to change that to the appropriate path, which is my external hard drive:  The real issue in this case is the Hyper-V not recognizing the external hard drive because of the fact that the Hyper-V Virtual Machine Management Service  was started before the external hard drive was attached via the USB.

I could have simply restarted the service or the laptop after I attached the external hard drive, and that would have fixed the issue. Hyper-V Correct Path After the change, I just performed a quick a restart and that fixed the connection issue: Hyper-V Connection Issue Fixed

CORRECTION: The path setting described above has nothing to do with this connection error. The Hyper-V Setting path, illustrated above, has nothing to do with the Hyper-V configuration not finding the correct path. This path setting tells Hyper-V where you want the succeeding virtual machines stored. For more information on this “Failed to lock virtual machine’s configuration” error, please read: Why is my virtual machine “Off-Critical”?

03/03/2014
by Marlon Ribunal
1 Comment

Virtual SQL Playground Adventure: Building The Virtual Environment

Few blog posts ago, I decided to build a virtual environment for a personal SQL Server sandbox. So, the Virtual SQL Playground Adventure blog series came to life.

In the introduction I promised to document everything but did not do a follow up blog post after that. That doesn’t mean [...] Continue Reading…