Sql-Server

Shortcut: Using "surrounds with" snippets in SQL Server Management Studio

In previous posts, I’ve been talking about how to use snippets in SQL Server Management Studio (SSMS) and how to create your own. There are several types of snippets and one of the special types of snippets that I want to mention are the “surround with” snippets.

If you look at the following block of code:

Imagine that you want to execute the four highlighted lines only when a condition is true. If I hit Ctrl-K and Ctrl-S while they are highlighted, I’m prompted with this:

2018-08-02

SDU Tools: ExecuteJobAndWaitForCompletion

You can execute SQL Server Agent jobs from within T-SQL code. The procedure sp_start_job can do that.

That’s all well and good but notice that it doesn’t say “execute job”; it just says “start job”. The command starts the execution of a job but has no interest in when it completes.

Sometimes, you need to be able to start a job and wait for it to complete before taking a following action.

2018-08-01

Upcoming: Database on a Diet in Auckland (Taming a large database)

Just a reminder that as well as the pre-conference day and the keynote, I’ll be presenting my Database on a Diet session at SQL Saturday in Auckland. I regularly work with some pretty sizable databases at tier-one financial (and other) organisations and they all struggle with the size of their databases. It’s easy for the size of a large database to get out of control.

In the example in this session, I’ll  describe the techniques I’ve used to reduce an operating 3.6TB database to just over 500GB, while increasing its performance. The session will include a deep dive into compression and other related techniques within SQL Server.

2018-07-31

SQL: What is sp_reset_connection and should I care about it?

Anyone who’s ever traced activity against a SQL Server will have no doubt seen a large number of commands where the procedure sp_reset_connection has been executed. Yet, this command won’t appear anywhere in the source code of the applications that are running.

As an example of why this occurs, one of the most common data access technologies that is used to connect applications to SQL Server is ADO.NET. It has a SqlConnection object that represents a connection that can be opened to a SQL Server instance. In the design of the SqlConnection class, the architects of it were grappling with two big issues:

2018-07-30

Shortcut: Using bookmarks in SQL Server Management Studio

In a previous post, I was discussing how outlining can be helpful with navigating around within a large T-SQL script file.

If you were trying to do that within a Microsoft Word document, the most common thing to use is bookmarks, and SQL Server Management Studio (SSMS) has them as well.

Bookmarks are simply placeholders within a script. (They can also apply to other types of document within SSMS). Where I find them very useful is when I’m working in two or three places within a long script at the same time. Perhaps I’m working on a function, and also on the code that calls the function. By using bookmarks, I’m not flipping endlessly around the script file, and can jump directly from placeholder to placeholder.

2018-07-26

SDU Tools: DropTemporaryTableIfExists

I regularly find myself writing repetitive code in T-SQL. Some things are best done by just creating code snippets but we’ve also added several others to our free SDU Tools for developers and DBAs.

One straightforward one is DropTemporaryTableIfExists.

This just wraps all that’s needed to remove a temporary table if it exists. The nice thing with this procedure is that you can call it before creating a temporary table, and call it again after you finish using the temporary table, as shown in the main image above.

2018-07-25

Upcoming: Precon for SQL Saturday in Auckland

At the end of the month, I’ll be delivering a pre-conference session in Auckland on Friday 31st of next month (August). Would love to see you there. Details are as follows:

Developing SQL Server Applications that Perform

So many SQL Server applications today are so slow, even simple ones, yet the product is capable of amazing performance, even in tier-1 organizations. Greg spends his life working with SQL Server developers (from the smallest startup software houses to many large tier-1 organizations), working with them to get their applications flying.

2018-07-24

SQL: What's in a (default) name?

I often see people creating databases in SQL Server and not specifying the name of defaults they are applying to columns. They define a column like this:

And there are general reasons why this makes sense. For example, a column can only have one default, so what does the name matter anyway?

There are two reasons:

Dropping columns

In SQL Server, you’ll find that if you go to drop either of those columns, you’ll see something like this:

2018-07-23

Shortcut: Code outlining in SQL Server Management Studio

For some years now, SQL Server Management Studio (SSMS) has had the ability to use code outlining, the same way that other Visual Studio applications can.

This can be very useful when you are trying to navigate around a large script file.

The simplest usage is to collapse or expand a region of code. Note that in the following script, code regions have been automatically added by SSMS:

This allows us to click on the outline handles, and collapse the code:

2018-07-19

SDU Tools: DatesBetween - all dates between two dates in T-SQL

In our free SDU Tools for developers and DBAs, we have added many tools that help to manipulate dates.

When creating a date dimension (as part of dimensional modeling), you need to be able to get a list of all the dates between a start date and an end date. There are many other reasons why you might need to do this as well.

So we’ve added a table-valued function called DatesBetween to do just this. It takes a start date and an end date as parameters and returns all dates between. As well as the date values, it also numbers each of the dates.

2018-07-18