Sql-Server

T-SQL 101: 29 Calling user-defined functions in SQL Server T-SQL queries

Important concepts in any development are reusing code, and abstraction. Look at the first WHERE clause here:

I’ve asked SQL Server to return rows where the CreditRatingID is the one that has the maximum rating. I might not know how to find the maximum rating but if there is code that finds it for me (i.e. dbo.GetMaximumRating()), I don’t need to know that.

This is an example of a function that retrieves a value, and then I can use the value in my own query, without having to copy all the code that’s needed, and without even having to know how it works.

2019-08-05

SQL: How to limit characters in a SQL Server string when varchar(n) won't work

In a recent post, I talked about how varchar(10) doesn’t mean up to 10 characters, particularly since the introduction of UTF-8 in SQL Server 2019.

So given I do want to limit the number of characters in strings at times, how exactly should I now do that?

A plan takes shape ?

One of my current plans is to do this:

Step 1: Start by forgetting that the number in brackets for the string data types is really relevant to a developer in any way.

2019-08-01

SDU Tools: Calculate Date Dimension columns in SQL Server T-SQL

When you’re building a data warehouse using SQL Server, one of the first things that most people start to design is a Date dimension. To make that really, really easy, in our free SDU Tools for developers and DBAs, we added a tool called DateDimensionColumns.

This tool is a table-valued function that takes a date, and returns a set of columns that many will find enough for a good Date dimension. You can see the list of returned columns in the main image above.

2019-07-31

T-SQL 101: 28 Working with NULL values (the lack of data) in SQL Server T-SQL queries

Look at the following query:

I’ve asked SQL Server to return orders where there are order comments. I did that by saying:

OrderComments IS NOT NULL

If I had said IS NULL, I would have returned all the orders with no comments. The interesting concept though, is NULL.

What is NULL?

The first thing to understand is that NULL isn’t a value. That’s why we can’t say:

WHERE OrderComments = NULL or WHERE OrderComments <> NULL

2019-07-29

SQL: Think that varchar(10) means 10 characters ? If so, think again!

If you read almost any book on the SQL language, you’ll see definitions where:

varchar(n)

means a varying length character data type, and where n is the number of characters it can store.

SQL Server 2019 changes things

If that’s how you’ve seen it, SQL Server 2019 is going to change your understanding. The product team have pointed out that that n was really the number of bytes, and that “it never was the number of characters”.

2019-07-25

SDU Tools: Working with SQL Server version, build, and release numbers

When you look at a SQL Server build number (actually a “Product Version”, you’ll see they have a format like this:

14.0.3256.1

The first number is the major version number. The second number is the minor version number. The third number is the build number. The fourth number is the release number.

These numbers are a little awkward to work with directly in T-SQL so, in our free SDU Tools for developers and DBAs, we added a set of tools that can do that for you.

2019-07-24

Opinion: RIP Microsoft Professional Program

Three years back, with much fanfare at a partner conference, Microsoft announced the Microsoft Professional Degree program. It was going to be a set of courses that you could take that would lead to one of their professional degrees.

Now here in Australia, you can’t just call something a degree, and I’m guessing that’s the same in the USA, so I wasn’t surprised when I noticed soon after I started with it, that the name had changed to the Microsoft Professional Program (MPP), and they’d dropped the “degree” word.

2019-07-23

T-SQL 101: 27 Using TOP WITH TIES to include matching rows in SQL Server T-SQL Queries

In the previous T-SQL 101 post, I showed the TOP operator. One of the challenges that comes up though, is if I say SELECT TOP (10) FROM dbo.Products ORDER BY Size, what if there are 5 items with the first Size, and 7 products with the second Size? Which rows get returned when I just ask for 10?

Perhaps what you want is the TOP (10) but then, when you get to the end of the 10, if the next one has the same value you might want to continue on. T-SQL has a way of doing this. We say WITH TIES as shown in the code below:

2019-07-22

SQL: How many indexes per table is too many when you're using SQL Server?

I wish query tuning was easy. Today, it’s not. One day machines will do it better than us, but today, doing it well is still somewhat of an artistic pursuit. You can teach most people the basics, but it takes a long time to get a really good “feel” for what needs to be done. However, something that I seem to differ with a lot of people on, is about how many indexes is too many?

2019-07-18

SDU Tools: List SQL Server user tables that are heaps (have no clustered index)

Most SQL Server tables should have a clustered index. There are some exceptions to this but it’s a pretty general rule, and if in doubt, you should follow it. (Note that this is not the same as having a primary key).

I regularly come across tables without clustered indexes for all the wrong reasons. So, in our free SDU Tools for developers and DBAs, we added a tool that can look for user tables that don’t have a clustered index. No surprise, it’s called ListUserHeapTables because a table without a clustered index is a heap.

2019-07-17