Understanding the Various ON DELETE Options in SQL Server Foreign Keys

It’s widely understood among SQL Server developers that foreign keys can prevent us from deleting a row from the parent table if there’s child table that references the key in that row.

But did you know that we can specify a different course of action in such cases?

For example, we could configure our foreign key to delete the child as well. Or we could set it to NULL, or to its default value.

These options are all available, thanks to the ON DELETE clause.

Continue reading

SQL Server OUTPUT Clause: Using Expressions to Analyze Changes to the Data

When we use the OUTPUT clause in SQL Server, we have the option of providing expressions instead of just the column names. For example, we could use an expression that compares the old price with the new price and returns the difference. Such data could be handy, depending on what your goals are.

Let’s dive straight into an example that uses an expression in the OUTPUT clause.

Continue reading

Fix “Ambiguous column name” in SQL Server (Error 209)

If you’re getting an error that reads “Ambiguous column name“, followed by a column name, in SQL Server, it could be that you’re performing a join on two tables, but you’re not using the table names or aliases when referring to columns within those tables.

To fix this issue, be sure to qualify the columns with their table names or aliases.

Continue reading

Fix “An aggregate may not appear in the OUTPUT clause” in SQL Server (Error 158)

If you’re getting an error that reads “An aggregate may not appear in the OUTPUT clause” in SQL Server, it appears that you’re using an aggregate function in the OUTPUT clause.

Aggregate functions are not allowed in the OUTPUT clause.

To fix this issue, remove any aggregate functions from the OUTPUT clause and try applying them to a table or table variable.

Continue reading

Using the INCLUDE Option on an Index in SQL Server

The INCLUDE option in SQL Server allows us to include non-key columns in a nonclustered index. These columns are not part of the index key (which SQL Server uses to order and search the index), but they are stored with the index pages.

The INCLUDE option can significantly improve query performance when additional columns are needed by a query but are not part of the index key.

Continue reading

Fix “Cannot insert the value NULL into column” in SQL Server (Error 515)

If you’re getting an error that reads something like “Cannot insert the value NULL into column ‘CatId’, table ‘demo.dbo.Cats’; column does not allow nulls. INSERT fails” in SQL Server, it’s because you’re trying to insert a NULL value into non-nullable column (i.e. a column with a NOT NULL constraint).

We can’t insert NULL values into non-nullable columns.

To fix this issue, insert a non-NULL value.

Continue reading

Using INSERT INTO with a CTE in SQL Server

Common Table Expressions (CTEs) are a useful way to simplify complex queries in SQL Server (and other SQL databases). More often than not, we’ll see CTEs that are used by SELECT statements, but this isn’t necessarily required. CTEs can be followed by SELECT, INSERT, UPDATE, DELETE, or MERGE statements.

This article provides a simple example of using INSERT INTO with a CTE in SQL Server.

Continue reading

How to DROP a Primary Key in SQL Server

Dropping a primary key in any RDBMS is a critical operation that requires careful consideration, especially when there are related foreign keys in other tables.

The decision to drop a primary key is not to be taken lightly.

That said, this article walks through the process of how to drop a primary key in SQL Server, while highlighting important considerations.

Continue reading

Fix Error “No column name was specified for column…” in SQL Server (Error 8155)

If you’re getting an error that reads something like “No column name was specified for column 2 of ‘SalaryTotals’” in SQL Server, it could be that you’re running a subquery or CTE, but you’re not providing a column name for an aggregate function, or for the XML or JSON result of the CTE.

To fix this issue, use an alias to define a column name for all aggregate functions and XML/JSON outputs in the subquery or CTE. Or if it’s a CTE, you can specify the column name using the CTE syntax.

Continue reading

Fix “Invalid column name ‘$action'” When Using the OUTPUT Clause in SQL Server (Error 207)

If you’re getting an error that reads “Invalid column name ‘$action’” in SQL Server, it could be that you’re using the $action argument of the OUTPUT clause for an operation that doesn’t support this argument.

Or it could be that the operation does support the argument, but you’re referencing it with the wrong syntax.

The $action argument can only be used with MERGE operations, and it must be done correctly.

To fix this issue, be sure to only use the $action argument in MERGE operations and to reference it with the correct syntax.

Continue reading