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

Example of PostgreSQL Automatically Creating a Nested Composite Type

Whenever we create a table in PostgreSQL, a composite type is automatically created behind the scenes. This composite type is based on the table that we created. Each column in the table becomes a field in the composite type.

If the table already uses a composite type, then the composite type that PostgreSQL creates will include that type in its definition, thereby creating a situation where we effectively have a nested composite type.

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

Prepared Statements in MySQL: A Complete Guide with Examples

In MySQL, prepared statements are a feature that allows us to execute the same SQL query multiple times with different parameters. They can improve performance, make our code cleaner, and help defend against SQL injection attacks.

In this article, we’ll explore prepared statements in MySQL, with examples and practical tips on how to use them effectively. We’ll also cover the importance of deallocating statements to free up resources.

Continue reading

How to Use the HANDLER Statement in MySQL for Faster Data Access

The HANDLER statement in MySQL provides a low-level mechanism to directly access storage engine-level functionality, bypassing some of the overhead associated with regular SQL queries. It can be especially useful for traversing a database in high-performance scenarios.

HANDLER is available for MyISAM and InnoDB tables, and can be used as a faster alternative to the SELECT statement.

Continue reading