SQL Server Error 280 occurs when you attempt to create a unique clustered index on a view that contains columns not derived directly from a base table. To fix this, you must remove any computed columns, expressions, or references to other views from the view definition, ensuring every column in the view maps directly to a physical column in a underlying table.
What You'll Need
- SQL Server Management Studio (SSMS) or another SQL query editor.
- Administrative permissions (sysadmin or db_owner) to modify views and create indexes.
- The T-SQL script used to define the current view.
Step-by-Step
- Identify the Problematic View: Locate the view where you are attempting to create the unique clustered index. If you are receiving this error during a deployment, check the
CREATE VIEWscript associated with the error. - Analyze the View Definition: Open the view definition and look for any of the following "non-base" elements:
- Computed columns (e.g.,
(Price * Quantity) AS Total). - Built-in functions (e.g.,
GETDATE(),LEFT(),CAST()). - Aggregates (e.g.,
SUM(),COUNT()) if you are not using a schema-bound view with specific aggregation rules. - References to other views (nested views).
- Computed columns (e.g.,
- Verify Schema Binding: Ensure the view was created
WITH SCHEMABINDING. A unique clustered index cannot be created on a view unless it is schema-bound. If it is not, you must drop and recreate the view with this clause. - Remove Non-Base Columns: Edit the
SELECTstatement of the view to remove any computed columns or expressions. Only columns that exist physically in the base tables should remain.- Example of what to remove:
SELECT FirstName + ' ' + LastName AS FullName - Example of what to keep:
SELECT FirstName, LastName
- Example of what to remove:
- Handle Required Computations: If the computed column is necessary for your application, move the calculation to a separate non-indexed view that references the indexed view, or handle the calculation in the application layer or a final
SELECTstatement. - Recreate the View: Execute the
ALTER VIEWorDROP/CREATE VIEWcommand with the cleaned-up column list. - Create the Unique Clustered Index: Run the
CREATE UNIQUE CLUSTERED INDEXcommand on the view. Since only base table columns are now present, Error 280 will no longer trigger.
Common Mistakes
- Using CAST or CONVERT: Many developers assume
CAST(Column AS VARCHAR)is a base column. SQL Server treats this as an expression, which triggers Error 280. - Nested Views: Attempting to create an index on View B, which pulls data from View A. Indexed views must reference base tables directly.
- Forgetting Schema Binding: Trying to fix the columns without adding
WITH SCHEMABINDING, which leads to a different error but prevents the index from being created. - Using Aliases for Logic: Creating a column alias for a complex logic string. Even if it has a simple name, the underlying logic makes it a computed column.
Tips
- Use a "Base View" Strategy: Create one view that contains only base columns and the unique clustered index. Then, create a second view (without an index) that references the first one to perform calculations and formatting.
- Check Determinism: If you are using functions in a view you intend to index, they must be deterministic. However, for Error 280 specifically, the requirement is strictly for base table columns.
- Keep it Lean: Only include columns in the indexed view that are absolutely necessary for performance gains. The more columns you add, the more overhead is added to every
INSERT,UPDATE, andDELETEon the base tables.
Troubleshooting
If you have removed all computed columns and still receive Error 280, check the following:
- Verify Table Types: Ensure the underlying tables are actual tables and not other views or table-valued functions.
- Check for Hidden Computed Columns: Check the base table definition. If the base table itself has a computed column and the view references it, SQL Server treats that as a non-base column. You must remove the reference to the base table's computed column.
- Refresh Metadata: In rare cases, metadata can become stale. Try running
sp_refreshview 'YourViewName'before attempting to create the index again. - Review Join Logic: Ensure you are not using
OUTER JOINs. Indexed views requireINNER JOINs. While this usually triggers a different error, it can complicate the view structure.
Frequently Asked Questions
Can I use a computed column in an indexed view at all?
No, not as part of the unique clustered index. The columns used to define the index must be base table columns. You can have computed columns in the view's SELECT list, but they cannot be part of the index itself.
What is a base table column?
A base table column is a physical column stored on the disk within a standard table. It is not the result of a function, a calculation, or a reference to another view.
Why does SQL Server require base columns for indexed views?
Indexed views physically persist data to the disk. To maintain this data efficiently during updates to the underlying tables, SQL Server requires a direct, deterministic mapping to the physical storage of the base table.
Does WITH SCHEMABINDING solve Error 280?
No. WITH SCHEMABINDING is a prerequisite for creating an indexed view, but Error 280 specifically refers to the type of columns being used. You need both schema binding and base table columns.
Will removing the computed column slow down my queries?
Usually, the opposite is true. By removing the computation from the view and creating a unique clustered index, you allow SQL Server to store the result set physically, which typically speeds up read operations significantly.