Indexed Views in SQL Server: A Complete Guide

Indexed views can significantly enhance the performance of complex queries in SQL Server. However, they come with several prerequisites and considerations that need to be carefully understood before implementation.

In this article, I provide an overview of indexed views, discussing their utility, prerequisites, and best practices, along with performance considerations and a simple example.

Continue reading

Fix “ALTER TABLE DROP COLUMN failed because one or more objects access this column” in SQL Server (Error 4922)

If you’re getting an error that reads something like “ALTER TABLE DROP COLUMN c2 failed because one or more objects access this column” in SQL Server, it looks like you’re trying to drop a column that’s accessed by another object.

This can happen when we try to drop a column that’s referenced by a schema-bound view. It can happen even when we only try to alter the columns too.

Continue reading

Fix “Cannot create index on view. It contains text, ntext, image, FILESTREAM or xml columns” in SQL Server (Error 1942)

If you’re getting an error that reads something like “Cannot create index on view ‘demo.dbo.v1’. It contains text, ntext, image, FILESTREAM or xml columns” in SQL Server, it appears that you’re trying to create an index on a view that uses an invalid data type.

Not all data types are supported when indexing views.

Continue reading

How to Truncate Tables with Foreign Key Relationships in PostgreSQL

When working with PostgreSQL databases, you may sometimes need to clear out all the data from tables that have foreign key relationships. This process, known as truncation, can be tricky when dealing with interconnected tables.

By default, if we try to truncate a table that is referenced by another table via a foreign key constraint, we’ll get an error that looks something like this: “ERROR: cannot truncate a table referenced in a foreign key constraint“.

You may have encountered this before finding this article. However, all is not lost. Below are two options for overcoming this issue.

Continue reading

Fix “The new name is already in use as a COLUMN name and would cause a duplicate that is not permitted” in SQL Server (Error 15335)

If you’re getting an error that reads something like “Error: The new name ‘c1’ is already in use as a COLUMN name and would cause a duplicate that is not permitted” in SQL Server, it appears that you’re trying to rename a column with a name that already exists in that table.

Basically, there’s already a column of that name in the table.

Continue reading

A Quick Look at the DROP_EXISTING Option in SQL Server

In SQL Server, we can use the DROP_EXISTING option of the CREATE INDEX statement to modify an existing index. It allows us to drop and rebuild an index in one atomic operation. This can be particularly useful when we need to make changes to an index, such as altering its columns, changing its properties, or rebuilding it with different options like fill factor or sort order.

Continue reading

Fix “Cannot create index on view. The function yields nondeterministic results” in SQL Server

If you’re getting an error that reads something like “Cannot create index on view ‘demo.dbo.v1’. The function ‘sysdatetime’ yields nondeterministic results…” and so on, in SQL Server, it looks like you’re trying to create an index on a view that returns nondeterministic results.

Indexes can only be created on views that return deterministic results.

Continue reading

How to View your Current SET Options in SQL Server

SQL Server provides various SET options that control the behavior of our session. These options can impact query execution and results. Also, some features in SQL Server rely on certain SET options to be configured in a certain way (for example indexed views).

Getting the current SET options configuration is not as straightforward as one might think. We need to do a bit of trickery.

Let’s look at how to view our current SET options.

Continue reading