Comparing DB objects in Powershell
I got a request today to compare 2 DBs and see if they’ve got all the same tables. It seemed easy enough since I’ve got the redgate Schema Compare tool, but it actually isn’t. See, for some reason Schema Compare doesn’t work very well on my box, and it’s a big overkill for what I needed. All I really needed was to see if the same tables existed. I wasn’t interested in comparing their actual definitions. So I thought I’d give Powershell a try.
I started by picking my cmdlet, which was going to be Compare-Object. I use its alias ‘Diff’. Now, this cmdlet isn’t typically used for comparing DB objects, but there’s nothing that says it can’t be. After giving it a little thought I decided to write the following code to get my comparison. Oh, and I’m just using sample DBs for my examples.
1 2 3 4 | $a = (cd sqlserver:\sql\localhost\default\databases\AdventureWorks\tables).dir; $b = (cd sqlserver:\sql\localhost\default\databases\AdventureWorksLT\tables).dir; Diff -reference ($a) -difference ($b) -property name |
OK, that’s the base-level syntax you need to do a comparison on table names. That code will give you the following results.
The left arrows indicate tables that are only found in the reference object ($a), while the right arrows indicate objects that are only in the difference object ($b). And that’s really it. The only thing you need to know is that while the differences are outlined, objects that appear in both sets aren’t listed. So your best bet would be to have an empty result set because that would mean that all of the objects exist in both places.
Now, keep in mind also that I only matched on the name property. So there are opportunities for this to go wrong because I’m not taking schema into account. If I wanted to take schema into account though, I would just add it to the property list like this:
1 2 3 4 | $a = (cd sqlserver:\sql\localhost\default\databases\AdventureWorks\tables).dir; $b = (cd sqlserver:\sql\localhost\default\databases\AdventureWorksLT\tables).dir; Diff -reference ($a) -difference ($b) -property schema, name |
This time you notice that the Address table shows up twice in different schemas. And in the first result set the Address table wasn’t there, so that means that the Address table was considered a match in both DBs because we weren’t taking schema into account.
However, there’s one final piece that you may find useful. What if you want to see all the objects, even the ones that match, you can throw in the -IncludeEqual flag. I’ll run the original Diff to show you that now the Address table shows backup and is indeed considered equal when you compare it just by name.
1 2 3 4 | $a = (cd sqlserver:\sql\localhost\default\databases\AdventureWorks\tables).dir; $b = (cd sqlserver:\sql\localhost\default\databases\AdventureWorksLT\tables).dir; Diff -reference ($a) -difference ($b) -property name -IncludeEqual |
Now that we’ve got all the plumbing worked out you can apply this to SPs, functions, or whatever you like.
It won’t give you an actual schema compare against these objects, but it’s really helpful in quite a few instances to be able to just see which objects exist.
That’s it for this time.
Incoming search terms:
- powershell sql server compare
- powershell script to compare two databases
- compare SQL Server database schemas with powershell
- powershell sql server compare two tables
- powershell sql schema compare
- powershell sql compare
- powershell sql schema diff
- compare database objects powershell
- powershell to compare database
- sql compare tool power shell
2 Responses to Comparing DB objects in Powershell
The best database career advice you’ve never heard!
Become a DBA. Become a BETTER DBA. Use the Roadmap.
The DBA Roadmap Seminar is 7 MP3 tracks (over 5 hours!) of insider guidance on your database career. We'll teach you how and what to study as a DBA, weigh in on controversial resume debates, teach you to recognize a worthy recruiter, and discuss the new professionalism of interviews. Also some bonus materials, PDF companion guides, and really spiffy intro music!
Once your $99 PayPal payment is submitted, you'll get the download link in e-mail! (Download is a 370Mb ZIP file.)
Visit www.DBARoadmap.com for info, forums, and more!
Tags
Architecture blunders Camtasia Career Coding Standards CPU data generator DBA Development disaster firewall Idiots Inside SQL Server Interview ITBookworm Jobs Kalen Delaney Katmai Ken Henderson LiteSpeed MidnightDBA PASS Summit 2011 Pinnacle politics Powershell Powershell Hero Query techniques Red-Gate reporting restore SQL Express 1433 SCM SQL Server SQLServerDVD.com SSIS SSRS Training troubleshooting TSQL Tuesday tuning Tutorials Video post videos YukonInterview: Kalen Delaney!
Archives
ITBookworm.com!- Reading: Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions by Itzik Ben-Gan
- Repost: “Problems with my new book” by Grant Fritchey
- Announcing SQL Server 2012 Query Performance Tuning by Grant Fritchey
- Quick Review: Logitech Trackball
- Book Review: Microsoft SQL Server 2008 Internals, by Kalen DeLaney et al













Sean,
Thanks for the post. I like that it’s a quick way to do a simple existence compare.
any reason you use this:
$a = (cd sqlserver:\sql\localhost\default\databases\AdventureWorks\tables).dir;
versus this:
$a = dir sqlserver:\sql\localhost\default\databases\AdventureWorks\tables
Just wondering if it is a style preference or a technical one.
No reason other than style. It’s the first way I thought of so I went with it.