Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix SQL Server Error 1759: Computed Column Issues

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server error 1759 occurs when you attempt to create or modify a computed column that references another computed column, or when the definition violates specific determinism and data type rules. This guide explains how to identify the problematic dependency chain and restructure your table design to resolve the error while maintaining data integrity.

What You'll Need

  • Access to SQL Server Management Studio (SSMS) or Azure Data Studio with sufficient permissions to alter tables.
  • The exact T-SQL script or database diagram showing the failing computed column definition.
  • A backup of the affected database or table before making structural changes.
  • Knowledge of the business logic behind the calculated values to determine if they can be flattened or persisted.

Step-by-Step

  1. Identify the offending column: Run the following query to list all computed columns in the target table. Look for columns where the definition field contains references to other computed columns.
    SELECT name, definition 
    FROM sys.computed_columns 
    WHERE object_id = OBJECT_ID('YourTableName');
  2. Analyze the dependency chain: If Column C depends on Column B, and Column B is also a computed column depending on Column A, this nested reference often triggers error 1759 during certain operations like index creation or schema changes. Note which columns are involved in the circular or deep nesting.
  3. Simplify the expression: Rewrite the definition of the final computed column so it references only base columns (non-computed columns). For example, if TotalPrice was defined as Subtotal + Tax, and Subtotal was itself a computed column (Qty * UnitPrice), change TotalPrice to (Qty * UnitPrice) + Tax. This removes the intermediate computed column dependency.
  4. Check for non-deterministic functions: Ensure the simplified expression does not use functions like GETDATE(), RAND(), or NEWID() unless the column is marked as PERSISTED and you understand the implications. Non-deterministic functions cannot be indexed, which may cause errors if you are trying to add an index to the computed column.
  5. Drop and recreate the column: Use the ALTER TABLE command to drop the existing computed column and add the new, simplified version. Replace YourColumnName with the actual name.
    ALTER TABLE YourTableName DROP COLUMN YourColumnName;
    ALTER TABLE YourTableName ADD YourColumnName AS (NewSimplifiedExpression);
  6. Test the change: Insert or update a row in the table to verify that the computed column calculates correctly without throwing error 1759. Check that any dependent views or stored procedures still function as expected.

Common Mistakes

One frequent mistake is attempting to persist a computed column that uses non-deterministic functions. While SQL Server allows creating such columns, you cannot create indexes on them, which leads to secondary errors if your application relies on indexed lookups. Another common issue is overlooking implicit conversions. If your computed column mixes data types (e.g., adding an integer to a decimal), ensure the result type is explicit to avoid unexpected rounding or overflow errors.

Developers often forget to check for circular dependencies. If Column A references Column B, and Column B references Column A, the table creation will fail immediately. Always trace the full path of dependencies before modifying the schema.

Tips

If you need complex calculations that involve multiple steps, consider using a view instead of a computed column. Views allow you to nest expressions freely without the strict limitations placed on computed columns regarding indexing and persistence. However, note that views do not store data physically, so performance may vary compared to persisted computed columns.

Use the PERSISTED keyword sparingly. It stores the calculated value physically in the table, which speeds up reads but slows down writes because the calculation must happen every time a base column changes. Only persist computed columns if they are frequently read and rarely updated.

Troubleshooting

If error 1759 persists after simplifying the expression, check for collation conflicts. If your computed column involves string concatenation from columns with different collations, SQL Server may throw an error. Resolve this by explicitly specifying a collation in the expression, such as COLLATE DATABASE_DEFAULT.

Verify that no foreign keys or constraints rely on the old column definition. Sometimes, dropping a computed column fails because an index or constraint depends on it. Drop those dependencies first, then modify the column, and finally recreate the indexes or constraints.

If you are working with a distributed transaction or linked server, ensure that the remote table’s schema matches your local expectations. Schema drift between servers can cause unexpected validation errors during column modification.

Frequently Asked Questions

Can I have a computed column that references another computed column?

Yes, but with restrictions. You can reference another computed column in the same table, but you cannot create an index on a computed column that references another computed column. Additionally, some operations like altering the base column may fail if the dependency chain is too deep or complex.

Why does my computed column fail when I try to add an index?

Computed columns must be deterministic to be indexed. If your expression includes non-deterministic functions like GETDATE(), you cannot index it. Even if deterministic, if the column references another computed column, indexing is prohibited. Flatten the expression to use only base columns.

How do I fix error 1759 in Azure SQL Database?

The resolution steps are identical to on-premises SQL Server. Simplify the computed column expression to remove nested references to other computed columns. Ensure the expression is deterministic if you plan to index it. Use SSMS or Azure Data Studio to execute the ALTER TABLE commands.

Is it better to use a trigger or a computed column for derived data?

Computed columns are generally preferred for simple arithmetic or string manipulations because they are automatically maintained by the engine. Triggers are more appropriate for complex logic involving multiple tables or external systems, as they offer more control over when and how data is updated.