Erland Sommarskog's home page
I'm an independent consultant who provides expert consulting in the SQL
Server area. I'm passionate about sharing my expertise with the community and the main purpose of this site is exactly that: Here you find articles, information about my past and upcoming presentations, links to videos of my talks and a few more things, all free for the community. I was first awarded Most Valuable
Professional on SQL Server by
Microsoft in April 2001, and I am very grateful to have been in the MVP programme since then.
"A Small Collection of Large Articles"
Through the years I have written a number of in-depth articles about specific topics related to SQL Server, mainly from a development perspective. I try to keep the articles up-to-date, and I very much welcome feedback, both about contents and spelling/grammar errors!
- The Curse and Blessings of Dynamic SQL.
How you use dynamic SQL, when you should – and when you should not. Latest revision: 2022-08-30.
- Dynamic Search Conditions. How to
write a stored procedure that permits users to select among many search
conditions, using both dynamic and static SQL. Latest revision: 2023-01-09.
- Slow in the Application, Fast in
SSMS? Ever experienced the situation that your query runs slow in your
application, but when you try it in SQL Server Management Studio, the response
is immediate? This article explains why this may happen, what parameter
sniffing is and suggests methods to analyse and address your performance
problem. Latest revision: 2023-01-09. Russian translation available.
- Packaging Permissions in Stored Procedures. How you can package a permission inside a stored procedure, so that users can perform privileged actions in a way you have control over. The main article covers the three techniques available: ownership chaining, certificate signing and impersonation with EXECUTE AS in three contexts: database-local, server-level and cross-database access. The article has an appendix where I show how these techniques can be used to solve specific problems. Latest
- Don't Let Your Permissions Be Hijacked! How a user with permissions to deploy triggers and stored procedures could get you to unknowingly execute malicious code that performs actions to the benefit of the attacker – and how you can defend yourself against these attacks. Latest revision: 2021-01-16.
- Error and Transaction Handling in SQL Server. A series in three parts and three appendixes that discuss the bewildering topic of what may happen when an error occurs in SQL Server and how you should implement your error handling. The first part is a short jumpstart for everyone, while Parts Two and Three are in-depth articles. The appendixes cover special topics: linked servers, the CLR and Service Broker. There are also two old articles for SQL 2000. Latest revision: 2022-12-26. Spanish and Russian translations of Part One available.
- How to Share Data between Stored Procedures.
Different ways of passing sets of data between stored procedures. Latest revision: 2021-09-03.
- Using Table-Valued Parameters in SQL Server and .NET. A primer on how to pass data through table-valued parameters from a .NET program to SQL Server with practical examples of how to load a comma-separated list and a file with master-detail data. Latest
- Using the Bulk-Load Tools in SQL Server. An article that covers BCP, BULK INSERT and OPENROWSET (BULK) from a functional point of view. You will learn about format files and how you can use them to tackle various file formats, but you will also learn what formats they are not able to handle. Latest revision: 2021-01-26.
SQL Short Stories
This is a section of articles that are shorter than the ones above and which are more focused on a specific problem. As with the articles above, I try to keep them updated.
- Arrays and Lists in SQL Server. This short story discusses how to handle comma-separated lists in SQL Server. There is also a longer article that discusses many methods for splitting lists into tables, which is a little more on the geeky side. Latest
- Monitor Exceptions in Your Database. Describes how you can monitor and view exceptions in your database with help of an extended-events session that collects them, and a view and a stored procedure that permits you to easily view the data. Latest revision: 2020-05-28.
- Using a Table of Numbers. This short story shows how a one-column table of consecutive numbers (or dates or hours etc) can be a powerful asset in your database to help you to write certain types of queries. Latest revision: 2020-09-16.
- Where Is that Table Used? This short story discusses various ways to find references to tables, columns etc in a database, but one of these ways is a simple utility to store your SQL code into a full-text indexed table, so that you can search the code efficiently. Latest revision: 2022-09-18.
Tips from SQLServerGeeks Magazine
These are shorter articles that I originally wrote for the SQLServerGeeks Magazine, and which the very nice people at SQLServerGeeks later republished on their blog. Thus, these articles have a sleeker design, but on the other hand, I don't make updates to these articles. Some of these tips are extracts from my other articles and presentations.
The Video Section
I don't have my own YouTube channel, but several of my presentations have been recorded and uploaded to YouTube by others. Here you find links to a selection of these videos. For some sessions there is more than one link, because I have given the session in different versions with regards to the length. For all videos, there is also a link for downloading slides and scripts for the presentation.
Utilities and Code
All of this code is in the public domain.
- beta_lockinfo. This is a stored procedure for lock-monitoring, giving
you a good snapshot so you can see who is blocking whom and what their current command is. Latest version: Version 28, 2022-12-30.
- Win32::SqlServer. Access SQL Server from Perl through OLE DB. Current version is 2.014, released
in June 2022.
- sp_sqltrace. A stored
procedure that takes an SQL batch as a parameter, sets up a trace, runs the batch, and then summarises the trace.
On SQL 2008 and later, you also get wait-stats information per statement. Optionally, you can get the execution plans for the statements. Rather than tracing your own process, you can snoop another spid. It is also possible to reanalyse a previously collected trace. A very useful tool for performance tuning developed by Lee Tudor that I am proud to host on my site. Latest version: Version 9, 2018-08-09.
- AbaPerls. A collection of tools for developing and deploying stored procedures,
tables and other SQL Server objects. You get a preprocessor, the ability to load stored procedures and get full object
checking directly. You will be able to build to update scripts from Team Foundation Server or SourceSafe and a lot more. This is something I originally wrote for the system I worked with at my previous employer and later client. Sadly, I have not been able to keep it up to date with all new features in SQL Server, and today it is more a showcase of things you can do. Current label is 1.0.0402, released April 2022.
Other Written Material
A mixed bag of items that don't fit in elsewhere.
- My SQL Server Wishlist. I've compiled a list of feedback items related to development that I wish to see implemented in SQL Server, the sooner the better. Read it and vote for the suggestions you like! Latest revision: 2021-12-04.
- Build Your Own Index. How you can make your LIKE searches run a lot faster - but with quite a bit of work. This page is a plug for the only book project I've been involved in. (And where our royalties go to War Child International.)
- Code sample for how to load an image or other binary file from VB .NET. A very simple example I put up many years ago, but which still may be useful.