Coolthing Of Theday

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 3 December 2013

What's the [table]diff? Diff'ing SQL Server tables with the Microsoft provided tablediff command line utility

Posted on 17:18 by Unknown

simple talk - SQL Tools - SQL Server tablediff utility

Rob Sheldon continues on his quest to explain all those command-line tools such as SQLCMD, Logparser, SQLIO and tablediff that are part of SQL Server. TableDiff is great for comparing tables, as when you run automated tests that check a result against a table of expected values. The best way to learn TableDiff is to see it in action and Rob talks you through several examples.

Among the many command-line utilities provided with SQL Server, one of particular note is tablediff, a handy tool that lets you compare the data in two tables in order to identify any discrepancies. Though originally intended primarily as a utility for troubleshooting non-convergence in a replication topology, tablediff can actually be used to perform a row-by-row comparison of any two tables that share a similar schema so is ideal for running automated unit tests for database routines. In addition, you can use the tool to generate the T-SQL script necessary to update the data in the second table to conform to the first table, if discrepancies are found.

Getting Started with the tablediff Utility

The best way to learn how to use the tablediff utility is to see it in action. This article includes a number of examples that demonstrate how to compare tables that contain both matching and non-matching data. The tables in our examples reside in databases on different SQL Server instances, one SQL Server 2012, the other SQL Server 2008 R2, both installed on the same local system. However, you can compare any two tables—within the same database or within different databases on the same or different servers—so if you want to try out these examples on your system, modify the T-SQL code and example tablediff commands as necessary to accommodate your setup.

Now let’s get started. First we’ll compare identical tables on the two SQL Server instances. To prepare the environment, I ran the following code on each instance to create the TableDiffDB database and the DiffTest1 table within the database:

...

The tablediff utility compares the data in the source table to the table in the destination table. In the example above (as well as those to follow), the localhost\sqlsrv2012 instance serves as the source and the localhost\sqlsrv2008R2 instance serves as the destination. Be aware, however, that when using the tablediff utility to compare two tables, the source table must be configured with a primary key or an IDENTITY or ROWGUID column.

NOTE: The tablediff utility uses Windows authentication to connect to SQL Server. However, you can also specify a username and password when connecting to the source instance, the destination instance, or both. For information about the login-related options (as well as details about other tablediff options), see the Books Online topic “tablediff Utility.”

...

Making the Most of the tablediff Utility

If you haven’t already discovered the tablediff utility, you’ll find it well worth your time to check it out. The tool is part of the SQL Server installation and is relatively simple to use. Just open a command prompt window and start typing. Not only can you compare tables across SQL Server instances, but you can also generate the T-SQL scripts necessary to update those tables. And you can run the utility from within scripts created in command shells such as PowerShell, thus letting you automate operations and reuse code. The tablediff utility might not meet all your needs when comparing tables, but in those cases where it does, you’ll likely find tablediff to be a valuable addition to your arsenal of tools.

tablediff Utility

The tablediff utility is used to compare the data in two tables for non-convergence, and is particularly useful for troubleshooting non-convergence in a replication topology. This utility can be used from the command prompt or in a batch file to perform the following tasks:

  • A row by row comparison between a source table in an instance of Microsoft SQL Server acting as a replication Publisher and the destination table at one or more instances of SQL Server acting as replication Subscribers.

  • Perform a fast comparison by only comparing row counts and schema.

  • Perform column-level comparisons.

  • Generate a Transact-SQL script to fix discrepancies at the destination server to bring the source and destination tables into convergence.

  • Log results to an output file or into a table in the destination database.

image

sigh... another day, another thing I didn't know. Why is it the older I get, the less I know? (What Yoda? You think, "Wisdom, on the road, you are...?" Byte me Yoda. You short little garden gnome looking...Oh damn. Did I say that out loud? ;)

Anyway...

There's a number of times where this free utility could have come in real handy. Now I just have to remember about it the next time I need something like it... :P

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in DBA, SQLServer | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Mr. 7,000! This is my 7,000th post...
    Before this post; After; 20 visits between taking these snaps? Oh wait, that's probably me searching for past related posts....
  • Rad Gate Post... Get your Red Gate Post here...
    simple talk - Melanie Townsend - Get a copy of the Red Gate Post We recently put together a newspaper of some of the best articles fr...
  • "Windows Server Essentials Media Pack" (DNLA Stream, HTML5 and Dashboard Media stuff)
    Microsoft Downloads - Windows Server Essentials Media Pack This pack enables the media streaming functionality for Windows Server 2012...
  • Windows Management Framework 4.0 (PowerShell 4, PowerShell ISE, Management OData, WMI, etc.) now available
    Keith Hill's Blog - PowerShell 4.0 Now Available You can get PowerShell 4.0 for down level operating systems now via the WMF 4.0 d...
  • Viasfora - Your new favorite Visual Studio Text/*ML Editing Extension?
    Winterdom - Introducing Viasfora A couple of days ago, I unveiled Viasfora , my latest attempt at building a decently packaged extensi...
  • "Windows Server [2012 R2]: The Best Infrastructure to Run Linux Workloads"
    In the Cloud - What’s New in 2012 R2: Enabling Open Source Software Part 4 of a 9-part series . ... There are a lot of great s...
  • [Hardware Review] Life with Haswell... Haswell/Harris Beach Intel SDS Ultrabook Review - Part 2
    "So Greg, how's life with Haswell been?" "Pretty Sweet! (Mostly)" I've been given an opportunity to review t...
  • Fuzzy Lookup Add-In for Excel (Insert lame "Fuzzy, wuzzy was an Excel..." snip here)
    Microsoft Downloads - Fuzzy Lookup Add-In for Excel The Fuzzy Lookup Add-In for Excel performs fuzzy matching of textual data in Exce...
  • Caliburn.Micro v1.5.0 released (CM gets Tasks, Async/Await and Share/Setting for RT... and bug fixes of course)
    Caliburn.Micro - Caliburn.Micro v1.5.0 "Release Notes This release fixes many bugs. It also adds support for Task and async/a...
  • Whoa there's allot of the free NOAA [resources]
    Government Book Talk - Be a NOAA-it-all with these FREE NOAA resources about the weather and oceans In the morning when I get on the e...

Categories

  • .Net
  • 3DPrinting
  • AFeedYouShouldRead
  • Agile
  • ALM
  • Amazon
  • Amiga
  • Analytics
  • Android
  • ASP.NET
  • Azure
  • BigData
  • bing
  • Blogging
  • Book
  • BookReview
  • BUILD
  • C
  • C#
  • C++
  • Career
  • Cat
  • cheatsheet
  • ClickOnce
  • Cloud
  • ComputerHardware
  • css
  • Data
  • DBA
  • DependencyInjection
  • Deployment
  • Design
  • Development
  • devops
  • DVCS
  • ebook
  • EDD
  • Education
  • EnterpriseLibrary
  • EntityFramework
  • Exchange
  • Expression
  • gadget
  • Game
  • GIT
  • Google
  • Government
  • Hadoop
  • hardware
  • HardwareReview
  • HaswellReview
  • HTML5
  • Humor
  • IE
  • IEExtension
  • IfAllElseFails
  • IIS
  • ILMerge
  • Image
  • Infographic
  • interview
  • InversionOfControl
  • Java
  • Javascript
  • Kinect
  • LightSwitch
  • LINQ
  • Linux
  • LosAngeles
  • Lucene
  • Lync
  • MEF
  • Metro
  • MicrosoftOffice
  • MicrosoftOutlook
  • Mono
  • MVC
  • MVVM
  • NetMon
  • NLP
  • NoSQL
  • NuGet
  • OData
  • OneNote
  • OpenXML
  • Paint.Net
  • Personal
  • Photosynth
  • Physics
  • portable
  • Poster
  • PowerShell
  • Preparedness
  • Presentation
  • Prism
  • PrivateCloud
  • RegEx
  • RemoteDesktop
  • Reporting
  • RIAServices
  • Science
  • ScienceFiction
  • Scratch
  • Scrum
  • ServiceBus
  • SharePoint
  • Silverlight
  • SimiValley
  • SPA
  • Space
  • SQLServer
  • Storyboard
  • Surface
  • SVG
  • SystemAdministration
  • T4
  • TeamBuild
  • TeamFoundationServer
  • TechEd
  • Training
  • TypeScript
  • UnitTesting
  • UnityApplicationBlock
  • Utility
  • Veteran
  • VirtualMachine
  • Visio
  • VisualBasic
  • VisualStudio
  • WCF
  • Web X.X
  • Webcast
  • WebFeed
  • WebMatrix
  • Windows
  • Windows7
  • Windows8
  • Windows8.1
  • WindowsHomeServer
  • WindowsLiveWriter
  • WindowsPhone
  • WindowsServer
  • WinRT
  • WiX
  • WMI
  • WOPI
  • WPF
  • XAML
  • XBox360
  • XboxOne
  • zombie

Blog Archive

  • ▼  2013 (500)
    • ▼  December (12)
      • Free Export DataSet/DataTable/List<t> to Excel (wi...
      • wxHexEditor, your new large, 16EBs (exabytes) larg...
      • Think your passwords rock? Check out Telepathwords...
      • Rad Gate Post... Get your Red Gate Post here...
      • [Humor] A familiar looking Project Team?
      • Slicing .NET - NSlice, the Python like Slice for .Net
      • WiX'ing in VS 2013. WiX 3.8 adds VS 2013 support, ...
      • The TFS Upgrade Guide gets... well... upgraded. v3...
      • Windows 2012 Server Edition Comparison Matrix and ...
      • RegEx'ing - The RefCardz
      • 200 C# Video Tutorials? 200 VB? JavaScript? PHP? C...
      • What's the [table]diff? Diff'ing SQL Server tables...
    • ►  November (61)
    • ►  October (65)
    • ►  September (38)
    • ►  August (47)
    • ►  July (75)
    • ►  June (39)
    • ►  May (40)
    • ►  April (42)
    • ►  March (39)
    • ►  February (42)
Powered by Blogger.

About Me

Unknown
View my complete profile