SQL Server Error Msg 4429 – View or function ‘%.*ls’ contains a self-reference. Views or functions cannot reference themselves directly or indirectly.

In this blog post, let’s learn about the error message “4429 – View or function ‘%.*ls’ contains a self-reference. Views or functions cannot reference themselves directly or indirectly.” in Microsoft SQL Server, the reason why it appears and the solution to fix it.

SQL Server Error Message

4429 – View or function ‘%.*ls’ contains a self-reference. Views or functions cannot reference themselves directly or indirectly.

Reason for the Error

You will get this error in SQL Server when you are trying to create a View or function but have used a table name which is same as the View Name. This error might be little confusing. Let’s have a look at it with an example.

  • You have an table by name “TestTable” under the “dbo” schema.
  • You have an view that already exists in SQL Server with the name “TestSchema1” that has the following
CREATE VIEW TestSchema1.TestTable
AS
	SELECT *
	FROM TestTable;

Assume that someone is creating a view with the same same as one of the table names without the schema name when querying the table name, SQL Server assumes that you are self-referencing the view. You will receive the below error.

29, Level 16, State 1, Procedure TestTable, Line 3 [Batch Start Line 6]
View or function ‘TestTable’ contains a self-reference. Views or functions cannot reference themselves directly or indirectly.

Interesting fact – When you run the same CreateView script as shown above, you’ll receive the SQL Error Message 4429 instead of “Object already exists” error. So confusing isn’t it ?

Solution

A simple solution to fix this error when creating views is to ensure that you are using the alias or schema correctly and avoid creating the views or functions with the same name as the table names.

    2 Comments

  1. Wesley Wisner
    February 1, 2023
    Reply

    Any update to this error and how to resolve it?

  2. February 6, 2023
    Reply

    Solution updated.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this blog post, let’s learn about the error message “1459 – An error occurred while accessing the database mirroring...
In this blog post, let’s learn about the error message “7937 – Columnstore index has one or more missing column...