Tag: SQL Data Compare

  • Using SQL Data Compare with Joins

    Recently a customer was asking about how they could move data with SQL Data Compare from one server to another. In this case, the customer had a complex join and WHERE clause they wanted to use with SQL Data Compare. This post will show a simple way to do this.

    The Source Data

    I can’t show customer data, but here is a mock up using the AdventureWorksDW sample database. I have this query that contains some information I want to sync from one database to another:

    SELECT c.FirstName,
            c.LastName,
            dd.FiscalQuarter,
            dd.CalendarYear,
            fis.OrderQuantity,
            fis.SalesAmount,
            fis.PromotionKey
            fis.SalesOrderNumber
    FROM dbo.FactInternetSales fis
         INNER JOIN dbo.DimDate dd
             ON dd.DateKey = fis.OrderDateKey
         INNER JOIN dbo.DimCustomer c
             ON c.CustomerKey = fis.CustomerKey
    WHERE ProductKey = 310
           AND dd.CalendarYear = 2011;

    This query includes multiple tables and a WHERE clause. In SQL Data Compare, there is no place to enter a query to use as a source for data. None of these sources allow for a query.

    2021-10-01 15_54_16-New project_

    I can, however, simulate this query in a view. I add a line above the code and I have a view.

    CREATE VIEW aDemoView AS
    SELECT c.FirstName,
            c.LastName,
            dd.FiscalQuarter,
            dd.CalendarYear,
            fis.OrderQuantity,
            fis.SalesAmount,
            fis.PromotionKey
            fis.SalesOrderNumber
    FROM dbo.FactInternetSales fis
         INNER JOIN dbo.DimDate dd
             ON dd.DateKey = fis.OrderDateKey
         INNER JOIN dbo.DimCustomer c
             ON c.CustomerKey = fis.CustomerKey
    WHERE ProductKey = 310
           AND dd.CalendarYear = 2011;

    I then need to check the option in the SQL Data Compare project to include views.

    2021-10-01 15_56_10-New project_

    Once I do this, I see my view, although it is unmapped. Note views are at the bottom of the dialog.

    2021-10-01 15_57_36-New project_

    Now I need a target.

    Make a Table From the View

    In the destination database, I need a comparison target. In this case, what I would do is take the definition of the view and use that to create a table. In this case, I’d start with the SELECT column list. I replace SELECT with CREATE TABLE, as shown here:

    CREATE TABLE DemoView
    (
            c.FirstName,
            c.LastName,
            dd.FiscalQuarter,
            dd.CalendarYear,
            fis.OrderQuantity,
            fis.SalesAmount,
            fis.PromotionKey,
            fis.SalesOrderNumber

    Next, I need to remove the aliases and round out the data types.

    CREATE TABLE DemoView
    (
            FirstName varchar(100),
            LastName varchar(100),
            FiscalQuarter tinyint,
            CalendarYear int,
            OrderQuantity int,
            SalesAmount NUMERIC(10,4),
            PromotionKey TINYINT,
            SalesOrderNumber VARCHAR(20)
            )

    Last, I will need a comparison key, so I’ll add the SalesOrderNumber as a PK.

    CREATE TABLE DemoView
    (
            FirstName varchar(100),
            LastName varchar(100),
            FiscalQuarter tinyint,
            CalendarYear int,
            OrderQuantity int,
            SalesAmount NUMERIC(10,4),
            PromotionKey TINYINT,
            SalesOrderNumber VARCHAR(20) CONSTRAINT DemoViewPK PRIMARY KEY
       )
    GO

    I execute this in my target database and this gives me a destination for the data.

    The SQL Data Compare Map

    Once I have a source and target, I can map them in SQL Data Compare. First, I select both objects in the Tables and Views tab and then click Map.

    2021-10-01 16_07_03-New project_

    This moves the two items up to the top pane.

    2021-10-01 16_07_26-New project_

    I need a comparison key in order to move data, and I select the “Not Set” on the left side. This gives me a place to set the comparison key. We need to know how to determine which rows are different from which other rows.

    2021-10-01 16_11_05-Comparison key

    Once this is done, I click the item in the main pane and click “Compare”.

    2021-10-01 16_11_13-New project_

    When this completes, I see my results. This table is shown as different, and I see the rows below.

    2021-10-01 16_15_19-SQL Data Compare - New project_

    All of these rows are only in the source, which is the left side. I can click “Deploy” and get a script to move all this data to the other server.

    Options

    In this case I had a fairly specific set of data in the view with a WHERE clause. If I wanted to keep this more generic, I could always use the WHERE clause in SQL Data Compare to select a set of data to move here.

    The other option would be to SELECT .. INTO  this data into a table on the source. I could load this into a table that I used for a comparison with the target. If I repeated this in the future, I could either truncate and reload this table, or just add to it, potentially with a different SELECT.

    In general, I prefer to bulk move information and control the data to move outside of SQL Data Compare. It’s easy to forget to change a WHERE clause somehow in a project. Much easier to control what I load into the source and then move everything to the destination.

    Summary

    The easy way to move complex data from source to destination is by putting the complex data into a source location. Either a table of some sort or a view. Then we can use SQL Data Compare to easily move this to a destination in another database.

    SQL Data Compare is an amazing product for syncing sections of data between databases. It might not work for all situations and can be slower with very large sets, but it’s a fantastic tool for DBAs.

  • Using Data Compare with Recent Data Only

    This is a post that looks at how to compare data changes in recent data. A customer recently asked me about looking at a table, and choosing specific data to compare. In this case, the data they were looking to compare was the most recent data.

    Scenario

    I decided to set up a quick scenario to showcase this for the customer. I created a table that has some data:

    CREATE TABLE [dbo].[DataWithTime](
         [myid] [int] IDENTITY(1,1) NOT NULL,
         [Mydata] [varchar](20) NULL,
         [mytime] [datetime] NULL,
      CONSTRAINT [DataWithTimePK] PRIMARY KEY CLUSTERED
    (
         [myid] ASC
    )
    GO
    INSERT INTO dbo.DataWithTime (Mydata, mytime)
    VALUES
    ( 'A', N'2021-01-22T12:42:33.213' ),
    ( 'B', N'2021-01-22T12:52:33.213' ),
    ( 'C', N'2021-01-22T13:02:33.213' ),
    ( 'D', N'2021-01-22T13:07:33.213' ),
    ( 'E', N'2021-01-22T13:12:33.213' )

    I put this in my sandbox database. I wanted a second copy of this same table, but with less data, in another database. I edited the insert statement to look like this:

    INSERT INTO dbo.DataWithTime (Mydata, mytime) 
    VALUES
    ( 'A', N'2021-01-20T12:42:33.213' ),
    ( 'B', N'2021-01-21T12:52:33.213' ),
    ( 'CC', N'2021-01-23T13:02:33.213' ),
    ( 'D', N'2021-01-24T13:07:33.213' ),
    ( 'EE', N'2021-01-25T13:12:33.213' ),
    ( 'F', N'2021-01-26T13:07:33.213' )

    Now I have two copies of my table, with disparate data. What’s different?

    Data Comparison Filters

    If I open SQL Data Compare, you get the default comparison. I’ll set this up with my two test tables:

    2021-01-25 11_12_32-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    When I do the comparison, I see the differences between the tables. As you can see, I edited two rows and added one.

    2021-01-25 11_13_25-SQL Data Compare - E__Documents_SQL Data Compare_SharedProjects_(local)_SQL2017.

    That’s great, and in a table of a few rows, this isn’t an issue. What if this table has a million rows? Or a billion? I don’t want to scan everything.I want to limit things.

    I can, if I click “Edit Project”.

    2021-01-25 11_17_43-SQL Data Compare - E__Documents_SQL Data Compare_SharedProjects_(local)_SQL2017.

    and then choose Tables and Views. I’ll see my table listed.

    2021-01-25 11_17_59-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    I can select the row with my table, DataWithTime, and then I can click the “Where clause” link in the upper right.

    2021-01-25 11_18_06-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    This pops up a dialog where I can enter a WHERE clause to be used for the table. I can set the same clause for both the source and target, or use separate ones. I’ll use the same one here.

    2021-01-25 11_18_51-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    I can click OK for this and then Compare now to re-run the project. This gives me the data compared, but without looking at any data before the 25th of Jan. Notice only two rows below instead of 3.

    2021-01-25 11_19_07-SQL Data Compare - E__Documents_SQL Data Compare_SharedProjects_(local)_SQL2017.

    Am I sure this still didn’t impact my SQL Server with a large query? This works great with 5 rows, but what about 1billion? Well, I ran the XEvent Profiler while I was editing the project, and then filtered this down to the SQL tools. When I do that, I see this:

    2021-01-25 11_21_16-ARISTOTLE - QuickSessionStandard_ Live Data - Microsoft SQL Server Management St

    The query being issued has my WHERE clause, which filters out data at the query processing level. This doesn’t guarantee a seek or limited reads, but if I have the column indexed, then I would get an efficient a plan as I could get.

    SQL Data Compare is fantastic tool for finding data differences. Comparing large volumes of data can be slow, but if you use filters, you can dramatically speed things up. If you haven’t tried SQL Data Compare, download an evaluation today and see what you think.

  • Recovering Data from a Backup with SQL Data Compare

    I had a customer recently ask about how to recover data from a backup file. I believe the request was for SQL Backup, but I knew there was another way, and I suggested SQL Data Compare. We used to have an object level recovery tool, but that wasn’t very popular. However, SQL Data Compare can handle this task and I’ll show you how.

    I’ve got a database, called Sandbox, and I take a backup of the database. After this, I delete some data, and I see then a few rows in a table. You can see 6 rows below.

    2021-01-19 17_21_15-SQLQuery1.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (56))_ - Microsoft SQL Server

    Now, I know there was more data in here. Imagine someone accidentally deleted data, and we want to get it back quickly. If I open SQL Data Compare, I can change the “Source” from the default, of a database, to a backup. I choose this from the dropdown near the “Source” text.

    2021-01-19 17_18_22-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    I can then add backup files. If I click the link, I get select a file from a file picker. Once I do this, I see this is a valid backup set. If needed, I could choose multiple files here.

    2021-01-19 17_18_32-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    Once I have this, I can then select my database in the Target. I’ll then click the Compare button.

    2021-01-19 17_18_46-(local)_SQL2017.SimpleTalk_1_Dev v (local)_SQL2017.SimpleTalk_5_Prod.sdc_

    Data Compare does it’s work and I end up with a list of identical, different, and missing objects. In this case, most everything is the same, but I have one object that is different. If I select the object, I see there are 4 missing rows.

    2021-01-19 17_19_03-SQL Data Compare - E__Documents_SQL Data Compare_SharedProjects_(local)_SQL2017.

    If I click the Deploy button, I get generate a script. This is the same process many people use with SQL Compare to generate code differences.

    2021-01-19 17_19_14-Deployment

    When this is done, I see my script contains a number of insert statements. If I’d changed some data, I would also see update statements here.

    2021-01-19 17_19_20-Deployment

    I can now run this script to recover my data.

    This is a quick and easy way to recover data. I do need a primary key for Data Compare to work, one of many reasons to ensure your tables have PKs. This also works well up to low GBs of data, but if you have more than that, this might not work well.

    I wouldn’t recommend this for every situation, but for many small “oops” problems, especially with lookup or smaller tables, this is a great way to recover missing data.

    If you haven’t tried SQL Data Compare before, maybe you want to give it a try today.