If you’re getting an error that reads something like “Cannot create index on view ‘MyView’ because the view is not schema bound” in SQL Server, it sounds like you’re trying to create an index on a view that’s not schema bound.
Indexes can only be created on schema bound views.
To fix this issue, define the view as schema bound before creating the index.
Example of Error
Suppose we create the following view:
CREATE VIEW MyView AS
SELECT
ProductId,
ProductName
FROM dbo.Products;
This view will work fine as it is. But if we try to create an index on it, we’ll get an error.
Let’s do that:
CREATE UNIQUE CLUSTERED INDEX IDX_MyIndex ON MyView (ProductId);
Output:
Msg 1939, Level 16, State 1, Line 1
Cannot create index on view 'MyView' because the view is not schema bound.
As expected, we got an error. That’s because views must be schema bound if we want to create an index on it.
Solution
So the solution is to make the view schema bound. We can alter the existing view or drop it and create it again.
Let’s alter it:
ALTER VIEW MyView WITH SCHEMABINDING AS
SELECT
ProductId,
ProductName
FROM dbo.Products;
Now that the view is schema bound, let’s try creating the index again:
CREATE UNIQUE CLUSTERED INDEX IDX_MyIndex ON MyView (ProductId);
Output:
Commands completed successfully.
This time it worked.