Smart Systems for Home

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

SQL Server Error 587: Invalid Delayed CLR Type Fetch Token

· 7 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 587 occurs when the database engine attempts to access a Common Language Runtime (CLR) type using a fetch token that is no longer valid, usually because the underlying assembly was altered, dropped, or updated without refreshing the dependent objects. To resolve this, you must identify the affected assembly, drop the dependent types or procedures, and re-create them to generate a fresh, valid fetch token.

What You'll Need

  • SQL Server Management Studio (SSMS) installed and configured.
  • Administrative privileges (sysadmin role) on the SQL Server instance to modify assemblies and system settings.
  • Access to the original source code or the compiled .dll binaries for the CLR assemblies being used.
  • A full backup of the database before performing assembly modifications.

Step-by-Step

  1. Identify the Faulty Assembly: Start by checking the SQL Server Error Log to determine which specific CLR assembly is triggering the error. You can find this by navigating to Management > SQL Server Logs in SSMS. Look for the error 587 entry; it often accompanies a reference to a specific assembly name or a stored procedure that utilizes a CLR type.
  2. Verify the Assembly State: Run a query against sys.assemblies to ensure the assembly is actually present and marked as authorized. If the assembly was recently updated using ALTER ASSEMBLY, the internal tokens used by the SQL Server engine to map T-SQL types to .NET types may have become desynchronized.
  3. Drop Dependent Objects: You cannot simply refresh a token; you must break the link between the T-SQL layer and the CLR layer. Identify all stored procedures, functions, or user-defined types (UDTs) that reference the problematic assembly. Use the DROP PROCEDURE or DROP FUNCTION commands for each dependent object. Note that if you have many dependencies, you may need to script these objects out first so you can recreate them easily.
  4. Refresh the Assembly: If the assembly is corrupted or outdated, use the ALTER ASSEMBLY command to update it with the correct binary. If you are certain the binary is correct but the token is the only issue, you may need to drop the assembly entirely using DROP ASSEMBLY and then recreate it using CREATE ASSEMBLY. Warning: Dropping an assembly requires all dependent objects to be dropped first.
  5. Recreate the T-SQL Mappings: Now that the assembly is clean, recreate the stored procedures and functions you dropped in Step 3. When you execute the CREATE PROCEDURE or CREATE FUNCTION scripts, SQL Server generates a new delayed fetch token. This token acts as a pointer to the CLR type; by recreating the object, you ensure the pointer is aligned with the current version of the assembly.
  6. Verify the Fix: Execute the specific stored procedure or query that previously triggered Error 587. If the execution completes without the "invalid delayed clr type fetch token" message, the synchronization between the T-SQL metadata and the CLR runtime has been restored.
  7. Clear the Plan Cache: In some cases, SQL Server may have cached an execution plan that still references the old, invalid token. If the error persists immediately after recreation, run DBCC FREEPROCCACHE to clear the plan cache, forcing the engine to generate a new plan with the new token.

Common Mistakes

One of the most frequent mistakes is attempting to fix Error 587 by simply restarting the SQL Server service. While a restart clears some memory, it does not fix the metadata mismatch stored in the database's system tables. The fetch token is a persistent identifier; if the assembly version has changed but the T-SQL object has not been redefined, the token remains invalid across restarts.

Another common error is using ALTER ASSEMBLY without subsequently updating the dependent T-SQL objects. Many administrators assume that updating the binary is sufficient. However, because SQL Server uses "delayed fetching" for CLR types to optimize performance, the T-SQL object holds onto a token that may become stale the moment the assembly is modified. You must explicitly drop and recreate the T-SQL wrapper to refresh this link.

Users also frequently forget to check the TRUSTWORTHY property of the database. If you drop and recreate an assembly that requires external access (like accessing the file system or network), the assembly will fail to load if the database is not set to TRUSTWORTHY ON or if the assembly is not signed with a certificate. This creates a secondary error that can be mistaken for the original token issue.

Tips

To prevent this error in the future, implement a deployment script that always follows a specific order: Drop dependent T-SQL objects → Alter/Create Assembly → Recreate T-SQL objects. Never update a CLR assembly in a production environment without a script that handles the dependent objects in this sequence.

Use sys.assembly_modules to quickly find all T-SQL objects that depend on a specific assembly. This prevents the tedious process of searching through every stored procedure in the database manually. By joining sys.assembly_modules with sys.objects, you can generate a list of all DROP statements needed before you modify the assembly.

When working in development, consider using a versioning strategy for your assemblies. Instead of overwriting the same assembly, you can occasionally deploy a new version with a different name to test stability before migrating all production objects to the new version. This allows for a faster rollback if the new CLR types introduce unexpected behavior.

Troubleshooting

If you have dropped and recreated the objects but Error 587 still appears, check for "ghost" dependencies. Sometimes, triggers or views may reference the CLR type indirectly. Use sp_depends or query sys.sql_expression_dependencies to ensure every single reference to the assembly has been cleared. If one single trigger still references the old token, the engine may continue to throw the error during specific transaction sequences.

If the error occurs only on certain servers in a cluster or Always On Availability Group, ensure that the assembly has been deployed to all replicas. A common cause of intermittent Error 587 in mirrored environments is a mismatch where the primary node has the updated assembly but the secondary node is still running an older version. When a failover occurs, the T-SQL objects (which are replicated) try to use tokens that are invalid on the secondary's version of the assembly.

In extreme cases where the assembly cannot be dropped because of complex dependencies, you may need to put the database in single-user mode to perform a cleanup. This ensures no active sessions are holding locks on the assembly or the dependent types, allowing the DROP and CREATE sequence to execute without "object is in use" errors.

Frequently Asked Questions

What exactly is a CLR fetch token?

A fetch token is an internal identifier used by SQL Server to lazily load .NET types from an assembly only when they are actually needed during execution. This improves startup time and memory usage but creates a dependency that can break if the assembly changes.

Does this error mean my data is corrupted?

No, Error 587 is a metadata and runtime linkage issue. It affects the ability to execute code that relies on the CLR assembly, but it does not corrupt the actual data stored in your tables or the binary of the assembly itself.

Can I fix this without dropping the assembly?

Usually, no. Because the token is tied to the specific version and state of the assembly, the only way to ensure a valid token is to redefine the T-SQL objects that use it. If the assembly itself is the problem, it must be altered or recreated.

Will clearing the cache fix the problem permanently?

No. DBCC FREEPROCCACHE only removes the compiled plan from memory. While it might temporarily stop the error if the plan was the only thing holding the old token, the underlying metadata mismatch remains. The objects must still be recreated.

Why does this happen after a SQL Server update?

Major SQL Server updates or service packs can occasionally change how the CLR is hosted or how assemblies are validated. This can invalidate existing tokens, requiring a refresh of the assemblies and their dependent T-SQL wrappers.

Is it safe to turn on TRUSTWORTHY for all databases?

No, turning on TRUSTWORTHY is a security risk as it allows assemblies to potentially access resources outside the database. The preferred method is to sign the assembly with a certificate and create a login from that certificate with the necessary permissions.