Why This Error Happens
Error 6240 occurs when a CLR user-defined function marked with IsDeterministic = true and IsPrecise = true (required for indexable computed columns or indexes on computed columns) no longer meets the strict indexability requirements after the assembly is altered. This typically happens because the updated function logic introduces non-deterministic operations, floating-point imprecision, or external data access that SQL Server’s query optimizer cannot guarantee as safe for indexing. SQL Server 2025 enforces these checks more rigorously during ALTER ASSEMBLY to prevent index corruption or incorrect query results.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Function and Its Current Attributes
- Open SQL Server Management Studio (SSMS) and connect to the target instance.
- Run the following query to inspect the function’s current indexability flags:
SELECT name, is_deterministic, is_precise, is_system_verified
FROM sys.objects
WHERE name = '<function_name>';
- Confirm whether
is_deterministicoris_precisehas flipped to0after the code change.
Step 2: Review and Correct the CLR Function Source Code
- Open the Visual Studio or MSBuild project containing the assembly source (commonly under
C:\Projects\SqlClrAssemblies\). - Remove any non-deterministic calls such as
DateTime.Now,Guid.NewGuid(), random number generators, or external file/network I/O inside the function. - Ensure all floating-point operations use precise types (
SqlDecimalordecimal) instead offloat/doublewhere precision is required. - Re-decorate the method with the correct attribute:
[SqlFunction(IsDeterministic = true, IsPrecise = true, DataAccess = DataAccessKind.None)]
public static SqlInt32 MyFunction(SqlInt32 input)
{
// deterministic, precise logic only
}
Step 3: Rebuild and Redeploy the Assembly
- Recompile the project to generate an updated
.dllfile. - Drop the existing indexes depending on the function before altering the assembly:
DROP INDEX IX_ComputedColumn ON dbo.MyTable;
- Run
ALTER ASSEMBLYwith the corrected binary:
ALTER ASSEMBLY MyAssembly
FROM 'C:\Projects\SqlClrAssemblies\MyAssembly.dll'
WITH PERMISSION_SET = SAFE;
Step 4: Recreate Dependent Indexes
- Once the assembly alters successfully, recreate any indexes or computed columns that relied on the function.
- Validate the computed column definition still references the function correctly:
CREATE INDEX IX_ComputedColumn ON dbo.MyTable (ComputedColumnName);
Alternative or Verification Steps
- Run
DBCC CHECKDBor querysys.assembly_modulesto confirm the function metadata now showsis_deterministic = 1andis_precise = 1. - If the function genuinely cannot meet indexability requirements (e.g., it must remain non-deterministic), drop the computed column index dependency entirely and convert it to a non-indexed computed column or a regular T-SQL scalar function instead.