Why This Error Happens
This error occurs because SQL Server requires that an indexed view and every table referenced within that view’s definition share the exact same schema owner. When a table is created by one user (or schema) and the view is created by another, SQL Server cannot guarantee schema binding consistency, which is mandatory for indexed views. This typically happens after objects are migrated, restored from backup, or created by different developers without enforcing a standardized schema ownership policy.
Step-by-Step Resolution Guide
Step 1: Identify the Mismatched Owners
- Run the following query to check object ownership:
SELECT o.name, s.name AS SchemaOwner FROM sys.objects o JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE o.name IN ('YourViewName', 'YourBaseTableName'); - Confirm that the
SchemaOwnercolumn differs between the view and its underlying table(s).
Step 2: Transfer Ownership to Match
- Use the
ALTER SCHEMAcommand to move the mismatched object into the correct schema:ALTER SCHEMA dbo TRANSFER OldSchema.TableName; - Repeat this for every underlying object referenced in the view definition until all objects reside under the same schema as the view.
Step 3: Drop and Recreate the View (If Necessary)
- If ownership mismatches were caused by legacy objects created under
saor a custom login, drop the view usingDROP VIEW SchemaName.ViewName; - Recreate the view ensuring the
CREATE VIEWstatement explicitly references the corrected schema name (e.g.,CREATE VIEW dbo.MyView WITH SCHEMABINDING AS...).
Step 4: Re-attempt Index Creation
- Execute the
CREATE UNIQUE CLUSTERED INDEXstatement again on the view now that ownership is unified. - Ensure the
SCHEMABINDINGoption was included in the view definition, as this is a prerequisite separate from ownership alignment.
Alternative or Verification Steps
- Verify the fix by querying
sys.indexesandsys.objectsto confirm the index was successfully created without throwing Error 1938. - If transferring schema ownership is restricted due to dependency constraints, consider creating a new unified schema (e.g.,
dbo) and migrating both the table and view into it simultaneously usingALTER SCHEMAin a single transaction to avoid orphaned dependencies.