T-Sql 101

T-SQL 101: 142 Merging data into a SQL Server table

Sometimes, you want to insert a row if it’s missing but update it if it’s already there. We had asked Microsoft for an UPSERT statement as that’s what it’s called in other databases.

What we got in SQL Server 2008 instead was a MERGE statement. It’s more flexible than an UPSERT statement.

In the example above, I’ve said I want to merge into the dbo.CinemaGroups table. Note that this statement also has an optional INTO word, just like INSERT does. That’s then considered the target table. Only one table can be modified in a single query, including with a MERGE query.

2025-04-09

T-SQL 101: 141 Updating data in a SQL Server table

An UPDATE statement is the way we change data in an existing row or multiple rows of a table. In the example above, I’ve said I want to update dbo.OrderContacts; that’s the table.

I asked SQL Server to set the OrderContact column to Terry Jones, but only where the OrderID is 1. That’s how we modify values in a table.

If I needed to modify more than one column, after Terry Jones, I could just put a comma, and then I don’t have to use the word SET again.

2025-04-07

T-SQL 101: 140 Truncating a SQL Server Table vs Deleting All Rows

Deleting rows from a table can take quite a long time because there is a lot of work going on under the covers. One way that you can just completely empty a table is by executing a TRUNCATE TABLE statement.

While this is fast, there are two issues with it:

First, if the table had delete triggers, and I used a DELETE statement, those triggers would fire. But if I say TRUNCATE TABLE instead, it just quickly nukes the entire contents of the table, by deallocating all the storage used for table rows. But those triggers wouldn’t fire.

2025-04-05

T-SQL 101: 139 Selecting rows into a new SQL Server table

In a previous post, I did an INSERT followed by a SELECT. When I do that, the table needs to already exist.

But what if I want to take the rows returned from a SELECT statement and use them to create a new table?

That’s what a SELECT INTO statement does. In the example above, I’ve taken a distinct list of OrderID and OrderContact values and used them to create a new dbo.OrderContacts table. If the table already exists, the statement will fail.

2025-04-03

T-SQL 101: 138 Inserting Rows Returned by a SQL Server Stored Procedure

Another way you can get rows to insert into a table is by executing a stored procedure or, in fact, even by executing dynamic SQL. Either way, these both use an EXEC statement.

In the example shown, I’m doing an INSERT into the dbo.CinemaGroups table. I’ve declared the two columns that are being inserted. The stored procedure returns the rows that I want to insert.

This is again fairly easy to troubleshoot because the EXEC clause can usually be executed by itself, and you can see the rows coming back.

2025-04-01

T-SQL 101: 137 Inserting rows with data from another SQL Server table

When you’re performing an INSERT, you can also get values from a query., rather than specifying the values in the INSERT statement.

In the example shown above, I’ve queried for a distinct list of order contacts and their phone number from the dbo.Orders table. I’m then inserting those values into the dbo.OrderContacts table.

These types of statements are easy to debug because I can just highlight the SELECT clause and run it to see what comes back.

2025-03-30

T-SQL 101: 136 Deleting Rows from a SQL Server Table

The DELETE statement is one of the most common SQL statements. We use it to remove rows from a table.

In the example, I’ve said to delete rows from the dbo.CinemaGroups table where the value in the CinemaGroupID column is greater than 3.

You need to make sure that your DELETE statements have WHERE clauses, unless what you’re intending to do is delete all the rows in the table. You don’t want to make the common mistake where people highlight a DELETE statement in SQL Server Management Studio and forget to include the WHERE clause as well.

2025-03-28

T-SQL 101: 135 What is a Foreign Key Constraint in SQL Server?

Foreign keys are mostly used where you store the primary key of another table, and want to check that it’s valid. This is used to ensure database integrity.

For example, I might have a CustomerID stored in an Orders table, and I want to make sure that the customer already exists. In the example above, I have a Courses table and I want to make sure that the CollegeID that’s stored for each course, actually matches a college that’s already in the Colleges table. I check that the CollegeID already exists by declaring a foreign key reference to that table.

2025-03-26

T-SQL 101: 134 What is a Unique Constraint in SQL Server?

In the previous post, I talked about check constraints. Another type of constraint in SQL Server is a unique constraint.

What I want to specify is that a value in a column is unique i.e., no other row has that same value in that column. While a check of the values can be manually requested, the constraint is normally checked when SQL Server is inserting or updating the row.

It is also possible to specify a unique constraint at the table level, rather than at the column level. You might want a combination of columns to be unique. Single column unique constraints can be declared at either the column or the table level.

2025-03-24

T-SQL 101: 133 What is a Check Constraint in SQL Server?

I previously described how column data types help define what you can put in a column. But sometimes I need to be more specific.

For example, if I have an ExternalCourseRating column and it’s an integer but it’s not allowed to be any value outside the range 1 to 4, I need to do more than just say it’s an integer because that could have a wide range of potential values.

2025-03-22