SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Comparing two SQL instances

There’s many a time in a DBA’s life where they’ll need to compare various aspects of two SQL instances against each other.

For instance, last week I was working with two instances that contained databases part of an Always On availability group and needed to ensure that all databases on the primary were on the secondary.

Now I know there’s a few ways to do this but I needed a quick and easy method as there were over 300 dbs involved.

The method I used to do this implemented the powershell cmdlet Compare-Object

What this does is pretty much what it says on the tin. The cmdlet takes two objects and compares them based on a input property (in this case it’ll be database name).

Here’s the code: –

$InstanceA = ''
$InstanceB = ''

$SqlQuery = 'SELECT name FROM sys.databases'

$DatabasesA = Invoke-SqlCmd2 -sqlinstance $InstanceA -query $SqlQuery
$DatabasesB = Invoke-SqlCmd2 -SqlInstance $InstanceB -query $SqlQuery

Compare-Object -ReferenceObject $DatabasesA -DifferenceObject $DatabasesB -Property "name"

Let’s run a quick test to show the output. Say I have two SQL instances with the following databases: –

If I run the script, I will get the following: –

This is telling me that Databases C & G exist in Instance B but not Instance A and that Databases E & I exist in Instance A but not in Instance B.

The script runs very quickly (even with a large amount of dbs) and gives a nice, easy to understand output that allowed me to work out which databases needed to be reseeded in my AG.

Now, that’s pretty cool. But what else can we do with this cmdlet?

How about we implement the dbatools Get-DbaSpConfigure to compare the configuration settings between these two instances?

Let’s say I want to compare the Maximum Memory settings between the instances. Here’s the code: –

$InstanceA = ''
$InstanceB = ''

$ConfigValue = "MaxServerMemory"

$InstanceAConfig = Get-DbaSpConfigure -SqlInstance $InstanceA | Where-Object {$_.ConfigName -eq $ConfigValue}
$InstanceBConfig = Get-DbaSpConfigure -SqlInstance $InstanceB | Where-Object {$_.ConfigName -eq $ConfigValue}

Compare-Object -ReferenceObject $InstanceAConfig -DifferenceObject $InstanceBConfig -Property "RunningValue"

And here’s the output: –

Telling me that Instance A’s memory is set to 2048MB and Instance B’s is set to 1024MB.

Nice! That means I can input any configuration setting outputted from
Get-DbaSpConfigure and compare across instances. Handy tool to have in the script deck imho ??

Thanks for reading!

The DBA Who Came In From The Cold

I’m Andrew Pruski and I am a SQL Server DBA with 5 years experience in database development and administration.

The online SQL Server community has helped me out immensely throughout my career, whether from providing reference material in blog posts, or answering my (sometimes obscure) questions on forums. So, to try and say thank you, I would like to contribute my own experiences in the hope that they could benefit someone out there.

So here’s my general ramblings and thoughts about working as a SQL Server DBA.

You can find me on twitter @DBAFromTheCold

If you have any feedback on my blog please send me an email to dbafromthecold@gmail.com.


Leave a comment on the original post [dbafromthecold.com, opens in a new window]

Loading comments...