SQL Server Error 434 occurs when you attempt to create a schema-bound view (usually for the purpose of creating an indexed view) that contains a function not permitted in the SELECT list. To fix this, you must identify the non-deterministic function in your query—such as GETDATE(), NEWID(), or certain user-defined functions—and move that logic out of the view definition and into the query that calls the view.

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 on the database.
  • The current T-SQL script used to create the view that is triggering the error.

Step-by-Step

  1. Identify the offending function: Review the SELECT statement within your CREATE VIEW script. Look for functions that return different values even when provided with the same input (non-deterministic functions). Common culprits include GETDATE(), CURRENT_TIMESTAMP, NEWID(), and RAND().
  2. Verify Schema Binding: Confirm if the view is created WITH SCHEMABINDING. Error 434 is specifically tied to schema-bound views because SQL Server must be able to persist the result set of the view into a physical index. Since non-deterministic functions change values over time, they cannot be physically stored in an index.
  3. Remove the function from the view: Delete the non-deterministic function from the SELECT list of the view definition. For example, if you have SELECT OrderID, GETDATE() as ReportDate FROM Orders, remove GETDATE() as ReportDate.
  4. Recreate the view: Execute the ALTER VIEW or DROP VIEW and CREATE VIEW command with the corrected code. Ensure the view still uses WITH SCHEMABINDING if you intend to index it.
  5. Create the Unique Clustered Index: Now that the non-deterministic function is removed, create the unique clustered index on the view. Use the following syntax:
    CREATE UNIQUE CLUSTERED INDEX IX_ViewName ON ViewName (ColumnName);
  6. Shift logic to the outer query: To get the functionality back, call the non-deterministic function in the query that selects from the view.
    Example: SELECT *, GETDATE() as ReportDate FROM ViewName;

Common Mistakes

  • Trying to use a User-Defined Function (UDF): Even if a UDF seems stable, if it accesses data in other tables or uses non-deterministic built-in functions internally, SQL Server will trigger Error 434.
  • Assuming "Deterministic" means "Constant": A function is deterministic if it always returns the same result for the same input. GETDATE() is non-deterministic because the "input" (the current time) is always changing.
  • Forgetting the Unique Clustered Index: Some users remove the function but forget that the primary purpose of a schema-bound view is often to support a materialized index. Without the index, the WITH SCHEMABINDING requirement is less strict, but you lose the performance benefits.

Tips

  • Use Computed Columns: If you need a value that is calculated but deterministic, consider adding a persisted computed column to the underlying base table instead of calculating it in the view.
  • Check Data Types: Ensure that any functions used in the view are compatible with the indexing requirements. For instance, certain TEXT or NTEXT types are not allowed in indexed views.
  • Keep Views Lean: Use indexed views only for the most expensive aggregations or joins. Move volatile data (like timestamps or random IDs) to the application layer or the final SELECT statement.

Troubleshooting

If you have removed all obvious functions like GETDATE() and you are still receiving Error 434, try the following:

  • Check Nested Views: Ensure the view is not selecting from another view that contains non-deterministic functions. Indexed views must reference base tables directly.
  • Inspect User-Defined Functions: If you are using a custom function, check its definition. If the function performs a SELECT statement against a table, it is considered non-deterministic by SQL Server and cannot be used in an indexed view.
  • Verify Collation: In rare cases, functions that rely on collation settings that can change can cause issues. Ensure your database collation is consistent.
  • Test without Schema Binding: Temporarily remove WITH SCHEMABINDING. If the view creates successfully, the issue is definitely a function that is incompatible with indexing.

Frequently Asked Questions

Why can't I use GETDATE() in an indexed view?

An indexed view stores its data physically on the disk. Because GETDATE() changes every millisecond, the stored index would be outdated immediately, making it impossible for SQL Server to maintain.

Does this error happen with regular views?

No. Standard views are essentially saved queries; they run the logic every time they are called. Error 434 only occurs when you use WITH SCHEMABINDING and attempt to create an index on the view.

Can I use a User-Defined Function if it only does math?

Yes, provided the function is marked as deterministic. However, most T-SQL UDFs that access tables are treated as non-deterministic. For complex logic, consider using an inline table-valued function instead of a scalar function.

What is the difference between deterministic and non-deterministic?

A deterministic function always returns the same output given the same input values. A non-deterministic function can return different results each time it is called, even with the same inputs (like RAND()).

Will removing the function slow down my reports?

No. Moving the function from the view to the outer query has negligible performance impact. The performance gain from the indexed view (which handles the heavy lifting of joins and aggregations) far outweighs the cost of calling a simple function in the final SELECT statement.