If you’re getting an error that reads something like “Cannot schema bind view ‘vEmployees’ because name ‘Employees’ is invalid for schema binding. Names must be in two-part format and an object cannot reference itself” it could be that you’re trying to create a schema bound view, but you’re not using a two-part format for names (such as the table names within the view).
To resolve this issue, be sure to use two-part names for any objects referenced in the view.
Example of Error
Here’s an example of code that produces the 4512 error:
CREATE VIEW vEmployees WITH SCHEMABINDING AS
SELECT
EmployeeId,
FirstName,
LastName,
Email
FROM Employees;
Output:
Msg 4512, Level 16, State 3, Procedure vEmployees, Line 2
Cannot schema bind view 'vEmployees' because name 'Employees' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.
We can see that the error message mentions that names must be in two-part format. This is referring to the names of objects in the view. In my case I’m referencing the Employees
table without specifying its schema.
Solution
We can easily fix this problem by using the two-part naming format (i.e. including the schema in the name):
CREATE VIEW vEmployees WITH SCHEMABINDING AS
SELECT
EmployeeId,
FirstName,
LastName,
Email
FROM Application.Employees;
Output:
Commands completed successfully.
This time it worked without issue; the view was created.
Note that I didn’t need to use a two-part name for the view itself. I only needed to do it for the table referenced inside the view. That said, we can just as easily use the two-part format for the view if we want.
For more about schema binding, see What Does Schema-Bound Mean?