The Bit Bucket

SQL: Finding the current default schema in a SQL Server session

SQL: Finding the current default schema in a SQL Server session

Each SQL Server user has a default schema. This the used in several ways. If I execute code like this:

CREATE TABLE Blah
(
...
);

I haven’t specified where that table will be created. People seem to assume it will end up in the dbo schema i.e. the table will be dbo.Blah but that’s not the case. It depends upon the user’s default schema. If that’s Sales, then the table Sales.Blah would be created instead.

2019-12-03

T-SQL 101: 46 Using LEFT, RIGHT, and SUBSTRING to extract substrings in SQL Server

T-SQL 101: 46 Using LEFT, RIGHT, and SUBSTRING to extract substrings in SQL Server

You might need to extract part of a string from a given string. T-SQL has a number of functions to help you do that. Take a look at these examples:

LEFT, RIGHT, and SUBSTRING are all functions that return parts of strings .

In the first example, we asked for 3 characters from the left of the string so SQL Server returned ABC.

The second example shows returning 2 characters from the right of the string, so we get KL.

2019-12-02

Snowflake for SQL Server Users - Part 17 - Data clustering

Snowflake for SQL Server Users - Part 17 - Data clustering

In SQL Server, most tables benefit from having a clustering key i.e. the column or columns that the table is logically sorted by. (Note: much old training material and documentation used to say “physical order” and that’s never been true). To do that, SQL Server stores the data within the pages in a sorted way, and maintains a doubly-linked logical chain of pages. In addition, it maintains pointers to the data by creating an index on it.

2019-11-29

SQL: Make column NOT NULL but don't check existing data in SQL Server

SQL: Make column NOT NULL but don't check existing data in SQL Server

A forum poster was asking how to make a SQL Server column NOT NULL, but not apply it to old data. But when you add NOT NULL, there’s no option to say “Just new data”. However, you can work around it to achieve the same goal.

When you add a constraint to SQL Server, you can say WITH NOCHECK. That says to not check existing data. You can add a constraint to ensure a value isn’t NULL.

2019-11-28

SDU Tools: Check Instant File Initialization (IFI) State for SQL Server

SDU Tools: Check Instant File Initialization (IFI) State for SQL Server

When SQL Server requests storage space from the operating system, the default action is for the OS to write zeroes over all the space before it’s presented to SQL Server to use. This is to ensure that the SQL Server process cannot read the data that was previously on that section of the storage (drive).

This can cause significant delays in at least two common situations:

  • A file needs to grow
  • Space needs to be preallocated during a database restore.

If this security issue isn’t a concern for you (if you have a dedicated SQL Server system, or you have a corporate SAN then it’s unlikely to be), then SQL Server has an option called Instant File Initialization that avoids all the writing of zeroes.

2019-11-27

T-SQL 101: 45 Upper and lower case conversion of strings in SQL Server

T-SQL 101: 45 Upper and lower case conversion of strings in SQL Server

If you need to convert strings to all upper case or all lower case, you can use the UPPER and LOWER functions in T-SQL as shown below:

These are simple functions and it doesn’t matter what those strings were to start with, it will just do the case conversion.

It’s also worth noting that if you want to make your SQL query text change to upper or lower case in SQL Server Management Studio, there’s a shortcut for that too. If you want to just quickly convert a string to all uppercase or all lower case, just highlight the string and hit control shift U for upper or control shift L for lower.

2019-11-25

Building a Great User Group – Part 1 - Don't Lose Newcomers

Building a Great User Group – Part 1 - Don't Lose Newcomers

Back in 2007, I wrote a book called Building Technical Communities. It was widely distributed at the time, and I’ve always been pleased with the feedback that I received on it.

For some time, I’ve been thinking about updating it. Recently, our MVP and RD lead Shiva has encouraged me to put my ideas somewhere they can be seen, so I’ve decided to add a blog post series that covers what I currently think about this. This is the first article. I’ve love to get your feedback.

2019-11-24

Snowflake for SQL Server Users - Part 16 - Primary and Foreign Key Constraints

Snowflake for SQL Server Users - Part 16 - Primary and Foreign Key Constraints

The Usual Situation

In general database terminology, a primary key is one or more columns that can be used to identify a particular row in a table. The key needs to be unique, and it can’t be null. An example would be a CustomerID or CustomerKey in a Customers table.

A foreign key is one or more columns that refer to a key in another table. A common example would be a CustomerID column in an Orders table. Foreign keys can be nullable and are checked when they contain a value.

2019-11-22

SQL: Why is my C drive thrashing when the databases aren't on it?

SQL: Why is my C drive thrashing when the databases aren't on it?

There was a poster on a forum recently who was puzzled about disk activity on his system. He had created his database files on non-system drives, yet when he checked the activity on the C drive, it was constantly thrashing. As SQL Server was the only application of any note on the system, he was wondering what on earth was going on.

There are things in the OS that will write to the drive but I think the most likely candidate is that tempdb is still on the OS drive.

2019-11-21

SDU Tools: Truncate trailing zeroes in SQL Server T-SQL

Often when I convert a decimal number to a string in T-SQL, I want the simplest version of the number. I really don’t want any trailing zeroes (i.e. at the end of the number).

In our free SDU Tools for developers and DBAs, we added a function to do just that: TruncateTrailingZeroes.

The function just takes one parameter: the number to be processed.

You can use our tools as a set or as a great example of how to write functions like these.

2019-11-20