Sql-Server

SQL: Create zombies (rows that won't die) in SQL Server by using INSTEAD OF triggers

I’ve seen a number of interesting discussions lately around how to handle deletes in database tables. One discussion in Stack Overflow had a variety of suggestions.

One easy option in SQL Server 2016 and later is to use temporal tables as they’ll keep the history for you. But there are a number of reasons why they might not be suitable.

Sometimes, you just want to stop rows being deleted, and to set an IsDeleted flag instead. If you don’t want to depend upon the application doing that for you, the easiest way to accomplish that is to use INSTEAD OF triggers.

2019-07-04

SDU Tools: Listing SQL Server user tables that have no primary key

SQL Server tables should have a primary key. I’m not talking about a clustering key (that’s a different topic) but a key that logically identifies each row, and isn’t ever NULL.

I have SQL purist friends that argue that a table without a primary key really isn’t a table.

When I’m reviewing database designs, one of the things I look for is the lack of a primary key on tables. It’s not just a purist issue. Without a primary key, other features like replication are problematic.

2019-07-03

Opinion: Do your bulk data manipulation in T-SQL, not in row operations in SSIS

I really love SQL Server Integration Services (SSIS). In fact, I wish Microsoft was giving it far more love than it is. So many of my clients use it, but unfortunately, not everyone uses it well, or for the right things.

One really common mistake is to perform lots of row-by-row logic within the SSIS packages. Let’s take an example of loading a fact table in a data warehouse as an example:

2019-07-02

T-SQL 101: 24 Selecting ranges of values with the T-SQL BETWEEN operator in SQL Server

The T-SQL BETWEEN operator let’s you specify a range of values. So in this case shown here:

I’m asking for rows where the DateCreated is between the first date and the second date. Notice how I’ve written the date here. We’ll talk about that a bit more in a later post. 2012, zero one, twenty-one is the 21st of January 2012. This is one of the formats that makes dates consistent.

2019-07-01

SQL: Don't use numeric constants in T-SQL ORDER BY clauses

In the T-SQL language you can specify a positive integer constant in an ORDER BY clause as I’ve shown in the main image above.

Please don’t do this !

For those that haven’t used this, the number refers to the position of the column in the SELECT list. If you do this, you are creating really fragile code and also code that’s really hard to read, particularly as the statements get more complex.

2019-06-27

SDU Tools: Finding the latest SQL Server builds for your version

I’ve lost count of the number of times I’ve seen people searching for details on what the latest SQL Server builds are. In a previous post, I mentioned that our free SDU Tools for developers and DBAs had a view called SQLServerProductVersions that showed all the SQL Server builds.

But that shows all the builds. What if you just want the latest details? We added a new view that works that out for you. It’s called LatestSQLServerBuilds. You can see it in use in the main image above.

2019-06-26

T-SQL 101: 23 Combining multiple WHERE clause predicates with AND, OR, and NOT in SQL Server

WHERE clauses can have more than one predicate.

In the case shown above, I’m saying

“where the description starts with hydro and not size equals 370ml”

The operators AND, OR, and NOT can be used to form a group of logical decisions that need to be made.

Now notice that in this case, it’s exactly the same as if I had said

“where the description starts with hydro and size is not equal to 370ml”

2019-06-24

SQL: Implementing queues in SQL Server the right way

There are many times when I need to use a queue in my database designs. Service Broker is a transacted queue that lives right in your SQL Server database. If you haven’t used it, or don’t even know why you should, read on.

The primary reason that I like to use queues is that they let you decouple different applications or different parts of applications. Let me give you some examples:

2019-06-20

SDU Tools: Add Weekdays in SQL Server T-SQL

In the last SDU Tools post, I mentioned new functions to determine if a day is a weekday or a weekend. (Based on Saturday / Sunday being weekends but easy for you to change if you have different weekends).  Another issue that I often have is the need to add a number of weekdays to a given date. To make it easy as well, we added a function AddWeekdays to our free SDU Tools for developers and DBAs.

2019-06-19

Data Modeling: Don't use money for storing money in SQL Server

I know that SQL Server has a number of data types that look like they could be used for storing amounts of money. Developers go looking for data types that have a decimal point in them, and there are a few:

Float - this one seems to get picked by people from a Java background. And it’s pretty much never the right choice. It doesn’t store values exactly because it’s an approximation. Just like we have numbers in decimal that we can’t write exactly (like 1/3 as a decimal is 0.33333 and so on), there are numbers in binary that we can’t store exactly. And that includes numbers like 0.1 (yes, ten cents). So using this data type for money is usually a newbie mistake, and the same applies to the potentially even more approximate real.

2019-06-18