Using SQL Compare to Revert a Stored Procedure

Imagine this situation, someone edits a stored procedure on a production server to “fix” something. However, they broke the procedure and you find out the next day. How do you fix this?

We’ll use SQL Compare since most of you don’t have version control (according to surveys), but you do (hopefully ) have backups. Let’s see how SQL Compare can help.

This is part of a series of posts on SQL Compare.

The Scenario

I get a call one morning that we have problems with a report. This report wasn’t producing the expected values. A user said they saw this:

2025-06_0134

However,the value the day before had been 2301.54. What was wrong?

Checking with SQL Compare

When I hear this, I think immediately two things:

  1. the data changed
  2. the code changed

Either is possible, but data is most likely. In this case, when I inquired, the user said no new orders were in the system. While I think humans can easily make mistakes when checking data, code is a possibility.

I asked people, but no one responded with a change being made. So let’s check. I know I have backups, so when I run SQL Compare, I change the source to a backup.

2025-06_0135

Next, I pick the “add backup files”

2025-06_0136

I choose my file(s) and confirm they are correct. In this case, just one file. I set the target to my database.

2025-06_0138

I run the comparison, and I see this. There are a few changes, but the one I’m looking for is my procedure. You can see the code is different.

2025-06_0139

Arguably, the new code is more correct, but if I need to revert, I can click the deploy button at the top. I’ll want to also just select the SalesReport item.

2025-06_0140

This will produce a script that I can run in SSMS. I can also just let SQL Compare run this. Note the old code is in the script.

2025-06_0141

Once I run this, the report works, or at least returns the expected results.

2025-06_0133

Summary

This is a short demo of using a backup as a comparison source against a database to revert code. In this case, I can see the old code and get that back to ensure my database functions as expected..

SQL Compare is an amazing tool that millions of users have enjoyed for 25 years. If you’ve never tried it, give it an eval today and see what you think.

Posted in Blog | Tagged , , | Comments Off on Using SQL Compare to Revert a Stored Procedure

Carrots and Sticks

Often,  we work on projects that might span a significant amount of time. Weeks, months, even years can be devoted to working on one system, or the same system. We may find that our motivation rises and wanes at different times. We get excited and motivated to get things done, and also become discouraged or distracted when challenges arise. Not all of us, or not all the time, but it is hard to maintain a high level of productivity and motivation over time.

For managers, it can be a challenge to keep a team moving over time and focused on achieving goals. Life gets in the way, people take vacations, staff changes, and other things are all issues that can distract a team. Keeping a team focused and productive can be a challenge for many managers. Perhaps especially challenging when someone hasn’t received enough training on how to manage and motivate others.

As tech workers, we could be motivated in a few ways. Perhaps there are goals, achievements, or rewards that spur us on to work harder. There could also be penalties or consequences if we don’t get enough done. There is always the threat of termination, but in practice, I see this as a last resort. More likely are subtle punishments such as boring assignments, more on-call work, or perhaps difficulties in getting vacation approved.

As a worker, do you want more carrots or sticks? Is the possibility of a reward or the threat of punishment a bigger motivator?

I’d like to say that carrots motivate me more, but as I think about it, I’m not sure. The carrot has to be significant to get me to change how I work. I think the stick also has to be fairly severe to force a change, but I think I’m often more motivated by the negative side of things than the positive. I’m sure that’s somehow from my childhood, but if I’m honest with myself, that’s what gets me moving more quickly. Though if there is too much of a stick, I’m quick to look elsewhere.

What about you? Carrots or sticks?

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged , | Comments Off on Carrots and Sticks

Adding a Named Default Constraint to a Table: #SQLNewBlogger

As part of a demo recently I was adding a default value to a new column with a simple DEFAULT and a value. Under the covers this creates a constraint, however, I want to ensure this is named explicitly and not auto generated. This post shows how to do this.

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

Setup

Let’s create a simple table like this one:

CREATE TABLE dbo.OrderHeader (
OrderHeaderID INT NOT NULL CONSTRAINT OrderHeaderPK PRIMARY KEY,
OrderDate DATETIME,
CustomerID INT
)
GO

Now I want to add a Created column to the table, with a default value of the current date and time. I decide to do this with an ALTER TABLE statement. In the past, I’ve done this with this code:

ALTER TABLE dbo.OrderHeader 
ADD Created DATETIME DEFAULT GETDATE()

The problem is this creates a constraint with a system generated name. If I deploy this code to different systems, I get different names. If I need to change the constraint or drop it, I have to query to find the name as it isn’t explicit. You can see this below.

2025-05_line0061

What I’d rather do is have a named constraint that makes sense to me. Let’s drop this column and do a better job. However, I cant’ just drop the column because I need to drop the constraint and that means I need to get the name.

2025-05_line0063

That’s the problem I’m trying to solve. Here is what I need to do.

2025-05_line0064

Now that I’ve dropped this, let’s add it back with an explicit name. This is simple SQL, and easy to add, just like I can do for Primary Keys. We’ll add a CONSTRAINT keyword and name before the default.

ALTER TABLE dbo.OrderHeader 
ADD Created DATETIME CONSTRAINT df_OrderHEader_Created_Getdate DEFAULT GETDATE()
GO

When I run this, now I see a named constraint.

2025-05_line0066

Note that I’ve named this for the column as if I need similar constraints in this table, they need to be uniquely named. This is in the database, not the table, as all constraints are stored in sys.default_constraints.

Do this and your database deployments go easier, especially across multiple systems.

SQL New Blogger

This is a simple thing, but it’s a good coding practice and better software engineering than allowing the system to name things. I explained how to do this and related this to a real issue in database development: deployments.

This post took me about 10 minutes, and it would likely take you about the same to start showcasing your knowledge. In today’s world, maybe you use AI to help you solve this problem and showcase that skill.

Posted in Blog | Tagged , , | Comments Off on Adding a Named Default Constraint to a Table: #SQLNewBlogger

Storage Enhancements

There was a time when I knew a lot about the various storage technologies available for a database. It was important when designing a server around the various requirements for size and performance balanced against the limitations of hardware.

The rapid growth of solid state storage and the adoption of storage area networks have changed the game for many of us. We no longer care or think about storage. It’s just a service that we consume in our databases, and while we might demand more IOPS capacity, we often don’t worry too much about how that’s provided to our systems.

I caught an article about IBM and a few enhancements to their offerings. They have a FlashSystem 5200, which is a way of taking their enterprise-class systems and packaging it in a way that organizations of all sizes can afford. In this case, they can get 1.7PB in a 1U package, which seems insane. I can remember working on an Enterprise 2U system that maxed out at 200GB worth of SCSI HDDs.

I wondered if this was a lot in today’s world. A few friends work for Pure Storage, and I’ve met some customers who were very impressed with their arrays. When I looked at a FlashArray//X70 from them, they get 2.4PB in 3U, so the IBM offering is more dense. In terms of IOPS, I’m sure they are both very fast, likely fast enough for most places I’ve worked.

What’s interesting is that I’ve found sysadmins way more concerned about specs and loving the Pure arrays more than the DBAs. DBAs (I think) are stuck with the hardware, and so they are always looking for what the bottlenecks are on a system and can I tune them down to a reasonable level. Or can I just get more storage or IOPs from whoever is in charge of storage?

The world of storage (and networking) has come so far in the last 30 years that the numbers today seem mind-bogglingly fast compared to even a decade ago. It makes me wonder why we have any performance problems at all. Unfortunately, all the hardware in the world can’t compensate for keeping too much data in expensive storage and writing bad code. A little query expertise and archiving structures would likely make all that hardware look as amazing in the real world as it appears in spec sheets.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | 1 Comment