Tag: Redgate

  • 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.

  • An Experiment in Building Culture

    While I have found that some aspects of my job with Redgate improved with our move to remote work this year, I think others have deteriorated. I have spent lots of time over the years trying to build relationships with people in lots of different departments. This year, I’ve gotten lots of time and more communication with those I often work with, but the connections across groups have been limited. I have worried this is affecting the great culture at Redgate.

    We have a group of people who spend time trying to deepen and strengthen team relationships. They have a budget each year and typically try to plan different types of activities for teams, departments, and groups inside the company. During the pandemic, we’ve had some competitions (quizzes, exercise goals, best picture), and they’ve been good, but for our end of year celebration, they really did a fantastic job.

    First, I saw Kendra post a picture of her end of advent calendar. Interesting, but I wasn’t sure what I thought. Plenty of other Redgaters posted similar pictures, which intrigued me, as I hadn’t gotten anything.

    Then I got a package with a note not to open anything before Dec 1. It contained my advent gifts, 24 boxes, and I spread them out on a table. I opened the first few myself before my family got interested. Day 3 was a mug, which my wife promptly grabbed, washed, and filled. Since then I’ve been looking forward to seeing what I get each day, with my gifts slightly different than what my UK colleagues are posting each day.

    Each year we usually have a large award ceremony, fancy dinner, and dancing with partners and colleagues. We can’t do this year, which I had expected to be a big loss when each office gets the chance to celebrate together. We’re separated, but this gift was an excellent substitute. Some teams jointly together get to celebrate our individual moments of joy with co-workers, bringing us together in a unique way when we can’t be together as a group.

    Culture is hard to build and maintain, but Redgate did a fantastic job, despite the challenges. Hopefully some of you find your organizations trying to do something that brings you together as well.

    Steve Jones

    Listen to the podcast at Libsyn, Stitcher or iTunes.

  • Setting Defaults for New SQL Compare Projects

    Recently I wrote about ignoring comments in SQL Compare. That seems like something I want to do in all my projects, so I went looking for how to set this as a default. It wasn’t obvious to me, but since I can ping the Redgate Software developers and support staff, I found an answer for this post.

    If I start SQL Compare by clicking a project, it opens and my settings are there. When I start SQL Compare, it brings up the New Project dialog, shown here:

    2020-12-01 10_01_10-New project_

    However, if I don’t want a new project, and close that, I have a basic interface. Going through the menus, I don’t see any way to set global options.

    2020-12-01 10_02_01-SQL Compare

    There are Application Options, but these aren’t anything to do with projects.

    2020-12-01 10_02_09-Application options

    However, I realized that I missed something a support person pointed out to me. On the options tab for any project is a button that says “Save as my defaults”.

    2020-12-01 10_03_55-(local)_SQL2017.SimpleTalk_1_Dev v localhost.SimpleTalk_1_Dev.scp

    I can set things from any project, or I can click the “My Projects” button in the toolbar, which gives me a list of projects (I don’t save many).

    2020-12-01 10_03_38-Microsoft Edge

    At the bottom of this is an “edit” button, which opens the familiar project dialog. I can click Options from there and see options.

    2020-12-01 10_08_22-(local)_SQL2017.SimpleTalk_1_Dev v localhost.SimpleTalk_1_Dev.scp

    When I do that, I can now set the items that I care about. The Ignore Comments is one, but there a few others I think cause issues. I do want to ignore encryption objects. I shouldn’t have the same ones  in dev as prod, so I’ll check that. Others:

    • Use database compat level (likely better than server version these days)
    • Online = ON
    • Ignore identity seed and increment (this could get changed)
    • Ignore WITH ENCRYPTION

    I can then click the “Save as my defaults”. This will ensure new projects have these options. If I have old projects I want to update, I can always click the “My defaults” in the project to load what I’ve saved. This animation shows how the button works.

    compare_options

    Now I can easily ensure that projects work as expected on my machine. Unfortunately, these defaults are stored in the registry, so this is a client by client basis, but you can ensure all your projects are set up the same by default.

    SQL Compare is a fantastic product to make it easy to see what has changed in a database. If you’ve never used it, give it a try today.

  • Ignoring Comments in SQL Compare

    Recently I had a client that wanted to know how they could use SQL Compare to catch actual changes in their code, but not have comments show up as changes. This is fairly easy to do, and this post looks at how this works.

    Setting up a Scenario

    Let’s say I have two databases that are empty. I’ll name them Compare1 and Compare2. I’ll run this code in Compare1:

    CREATE TABLE MyTable
    (   MyKey INT NOT NULL IDENTITY(1, 1) CONSTRAINT MyTablePk PRIMARY KEY
       , MyVal VARCHAR(100));
    GO

    CREATE PROCEDURE GetMyTable @MyKey INT = NULL
    AS
    IF @MyKey IS NOT NULL
         SELECT
               @MyKey AS MyKey, mt.MyVal
         FROM  dbo.MyTable AS mt
         WHERE mt.MyKey = @MyKey;
    ELSE
         SELECT mt.MyKey, mt.MyVal
         FROM dbo.MyTable AS mt;
    SELECT 1 AS One;
    RETURN;
    GO

    I’ll run the same code in Compare2 and then run SQL Compare 14 against these two databases. As expected, I find no differences.

    2020-11-30 14_59_33-

    I used the default options here, just picking the databases and running the comparison. Let’s now change some code. In Compare2, I’ll adjust the procedure code to look like this:

    CREATE OR ALTER PROCEDURE GetMyTable @MyKey INT = NULL
    AS
    /*
    Check for a parameter not passed in. If it is missing, then
    get all data.
    */
    IF @MyKey IS NOT NULL
         SELECT
               @MyKey AS MyKey, mt.MyVal
         FROM  dbo.MyTable AS mt
         WHERE mt.MyKey = @MyKey;
    ELSE
         SELECT mt.MyKey, mt.MyVal
         FROM dbo.MyTable AS mt;
    SELECT 1 AS One;
    RETURN;
    GO

    I can refresh my project, and now I see there is a difference. This procedure is flagged as having 4 different lines, as you see in the image below.

    2020-11-30 15_01_59-SQL Compare - E__Documents_SQL Compare_SharedProjects_(local)_SQL2017.SimpleTalk

    However, the procedure isn’t different. I’ve just added comments to one of the procs. You might view this as different, in terms of how you run software development, but to the SQL Server engine, these procs are the same. How can I avoid flagging this as a difference and causing a deployment of this code?

    Changing Project Options

    Redgate has thought of this. In the SQL Compare toolbar, there is an “Edit Project” button.

    2020-11-30 15_06_34-SQL Compare - E__Documents_SQL Compare_SharedProjects_(local)_SQL2017.SimpleTalk

    If I click this, I get the dialog that normally starts SQL Compare, with my project and the databases selected. Notice that there are actually four choices at the top of this dialog, with the rightmost one being “Options”.

    2020-11-30 15_06_40-(local)_SQL2017.SimpleTalk_1_Dev v localhost.SimpleTalk_1_Dev.scp_

    If I click this, there are lots of options. I’ve scrolled down a bit, to the Ignore section. In here, you can see my mouse on the “Ignore comments” option.

    2020-11-30 15_08_06-(local)_SQL2017.SimpleTalk_1_Dev v localhost.SimpleTalk_1_Dev.scp_

    I’ll click that, click Compare Now, which then refreshes my project. Now I all objects shown as identical. However, if I expand the stored procedure object, I can still see the difference. The difference is just ignored by SQL Compare.

    2020-11-30 15_09_36-SQL Compare - E__Documents_SQL Compare_SharedProjects_(local)_SQL2017.SimpleTalk

    This lets me track the differences, see them, but not have the project flag them for deployment. If I’m using any of the Redgate automation tools, the command line option for this is IgnoreComments, or icm. You can pass this into any of the tools to prevent comments from causing a deployment by themselves.

    This also works with inline comments. I’ll alter the procedure in Compare1 with this code:


    CREATE OR ALTER PROCEDURE GetMyTable @MyKey INT = NULL
    AS
    IF @MyKey IS NOT NULL
         SELECT
               @MyKey AS MyKey, mt.MyVal
         FROM  dbo.MyTable AS mt
         WHERE mt.MyKey = @MyKey;  -- parameter value filter
    ELSE
         SELECT mt.MyKey, mt.MyVal
         FROM dbo.MyTable AS mt;
    SELECT 1 AS One;   -- second result set.
    RETURN;
    GO

    The refreshed project sees the differences, but this is still seen as an identical object for the purposes of deployment.

    2020-11-30 15_17_15-SQL Compare - E__Documents_SQL Compare_SharedProjects_(local)_SQL2017.SimpleTalk

    If you are refactoring code, perhaps by just adding comments or clarifying something, you often may not want a deployment triggered just from changing the notes you leave for other developers. SQL Compare can help here, as can all the Redgate tools.

    I would recommend this option always be set, unless you have a good reason to allow comments to trigger a deployment.

    Give SQL Compare a try today if you’ve never used it, and if you have it, enable this in your projects.