The Bit Bucket

SQL Interview: 29 Add column to middle of SQL Server table

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

You need to add a new column to a table.

A developer insists that the column needs to be inserted into the middle of the existing table, not as a new column at the end.

2025-03-21

T-SQL 101: 132 Identifying the Last Value Inserted with Identity Columns in SQL Server

I showed how identity columns are a special type of constraint. They can be  int or bigint. Both work just fine. What you might need to know though, is the last value inserted automatically by SQL Server.

@@IDENTITY

For a long time, SQL Server only had a single option for this. It was the @@IDENTITY value.

The problem was that it could give you a value different to what you were looking for. The most common situation where this happened is if a trigger was set up. An INSERT trigger says when somebody does an insert, after it’s finished, execute this command as well. But what if that code in the trigger inserted a row somewhere else, perhaps to audit the inserts. The problem is that @@IDENTITY would return back the value from the second INSERT, not the one that you thought it was returning.

2025-03-20

SQL Interview: 28 When computed columns are calculated in SQL Server

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

You add a computed column to an existing table.

You then select from the table.

When is the value of the computed column calculated i.e., when the value is inserted or updated, or when the value is selected?

2025-03-19

SQL Server Administration for Developers and DBAs - new online on-demand course now available!

One of the most popular courses that we used to run in person was a SQL Server Administration course. Yesterday, I recently finished converting the course to an online format, and it was a big job.

I’ve had many people asking if we had this type of course. Administration is such an important skill.

Not just a single version course

We were also determined to stay with our tradition of making multi-version courses. We won’t just teach you about SQL Server 2022. Even though we’ve updated content to that version, we’ve included content for all relevant previous versions, and we show the progression across the versions.

2025-03-18

T-SQL 101: 131 What are Identity Columns in SQL Server?

In the previous post, I talked about how you could add default values for columns.

One special type of default value is called an IDENTITY constraint. These columns are one way to achieve automatic numbering in a column. There are many pros and cons about using these but at this point, I want to make sure you know how to use them.

The main difference from other columns is that you don’t put them in your INSERT statements. In fact, unlike other types of default constraint, you cannot just put the values in there. It will raise an error.

2025-03-18

SQL Interview: 27 Copy only backups in SQL Server

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Administration Level: Intro

Question:

You need to take an additional backup copy of a database to send to another site. You are considering using the Copy Only backup option.

What exactly is different about a Copy Only backup, compared to a Full backup?

2025-03-17

T-SQL 101: 130 What are Default Constraints in T-SQL?

Default constraints allow us to automatically provide values for columns when:

  • we don’t supply them ourselves
  • they are marked as NOT NULL so they are required

When you’re building an INSERT statement, you don’t have to give values for all columns. Columns that are declared as NULL aren’t mandatory, so you don’t have to supply those columns at all. Those columns can just be NULL.

But if you have a column that is declared as NOT NULL, so it requires a value, but you haven’t supplied it in the INSERT statement, then a default constraint can provide the value. This is configured as part of the table definition, or added as a constraint later by altering the table definition.

2025-03-16

SQL Interview: 26 Whole of data operations when adding columns in SQL Server

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Administration Level: Medium

Question:

You need to add additional columns to a table. You are concerned that you do not want to lock the table for long periods. You plan to avoid any operation that would rewrite every row i.e., avoid whole of data operations.

2025-03-15

T-SQL 101: 129 Inserting Multiple Rows at Once with VALUES clause in SQL Server T-SQL

Prior to SQL Server 2008, we could only insert a single row at a time. SQL Server 2008 added the ability to have multiple rows of data in the VALUES clause.

Each row is created from what are typically called row constructors. In the example above, I’m inserting 3 rows into the Cinema Groups table. The syntax allows for up to 1000 rows to be inserted at once.

Before you get to that number of rows though, you might exceed the maximum allowable length for a T-SQL statement, if you have a lot of columns, each with a lot of data. The maximum length of a statement is in theory 2GB but most people run into the batch size limit of 65536 * network packet size or about 268MB, first. Either way, it’s a lot of data in a single statement, and running into these limits is uncommon.

2025-03-14

SDU Tools: Reset Sequence using SQL Server T-SQL

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. Users that are new to working with sequences often don’t have simple options for resetting them back to a specific value. Many business intelligence ETL or ELT processes need to have a way to do this. So we added a tool that can help. It’s called ResetSequence.

The procedure takes three parameters.

2025-03-13