SQL Server Error 228 occurs when the SQL Server Common Language Runtime (CLR) cannot find a specific method within a loaded assembly that matches the signature defined in the SQL function or procedure. This is almost always caused by a mismatch between the .NET code (the DLL) and the T-SQL declaration, such as a changed method name, a modified parameter list, or a failure to redeploy the updated assembly to the server.
What You'll Need
- SQL Server Management Studio (SSMS) with administrative privileges.
- The source code for the .NET assembly (C# or VB.NET) used in the CLR.
- The compiled .dll file of the assembly.
- Knowledge of the specific function or procedure triggering the error.
Step-by-Step
- Identify the Mismatch: Run the query that triggers the error. Note the exact name of the function or procedure and the parameters being passed. Error 228 specifically indicates that while the assembly is found, the "entry point" (the method) inside that assembly is missing or has a different signature.
- Verify the .NET Method Signature: Open your source code and locate the method being called. Ensure that:
- The method is marked as
publicandstatic. - The method name exactly matches the name specified in the
CREATE FUNCTIONorCREATE PROCEDUREstatement (case-sensitivity depends on your database collation). - The number of parameters, their order, and their data types align perfectly with the SQL declaration.
- The method is marked as
- Recompile the Assembly: If you made changes to the method signature in the source code, rebuild the project in your IDE (such as Visual Studio) to generate a new .dll file.
- Drop the Existing SQL Object: Before updating the assembly, you must remove the T-SQL wrapper that references it. Run:
DROP FUNCTION [schema].[FunctionName];(orDROP PROCEDURE). - Update the Assembly: You cannot simply overwrite an assembly if it is being referenced. Use the
ALTER ASSEMBLYcommand to upload the new version:ALTER ASSEMBLY [AssemblyName] FROM 'C:\Path\To\Your\Assembly.dll';
Note: The file path must be accessible by the SQL Server service account. - Recreate the SQL Object: Execute your
CREATE FUNCTIONorCREATE PROCEDUREscript again. Ensure theMETHOD_NAMEparameter in the script matches the updated .NET method name exactly. - Test the Execution: Run the original query again to verify that the "Method of type in assembly does not exist" error is resolved.
Common Mistakes
- Case Sensitivity: Forgetting that some SQL Server collations are case-sensitive, leading to a mismatch between
MyMethodin C# andmymethodin T-SQL. - Parameter Type Mismatch: Using a
longin C# but declaring it as anINTin SQL (which expects a 32-bit integer). This changes the method signature, causing SQL Server to report that the method does not exist. - Forgetting 'static': Defining the .NET method as an instance method rather than a static method. SQL Server CLR requires methods to be static to be called as functions.
- Partial Deployment: Updating the .dll file on the server disk but forgetting to run
ALTER ASSEMBLYwithin SQL Server. SQL Server loads the assembly into memory; changing the file on disk does nothing until the assembly is re-registered.
Tips
- Use Strong Names: Always sign your assemblies with a strong name key. This prevents versioning conflicts and is required if you are setting the assembly permission set to
UNSAFE. - Keep a Deployment Script: Maintain a single SQL script that contains both the
CREATE ASSEMBLYand theCREATE FUNCTIONstatements. This ensures that the assembly and the wrapper are always deployed as a synchronized pair. - Check Assembly Version: Use
SELECT * FROM sys.assembliesto verify the version and load date of the assembly currently residing in the database.
Troubleshooting
If you have updated the assembly and the method signature is correct, but Error 228 persists, try the following:
- Clear the Cache: In some rare cases, the execution plan may be cached. Try running
DBCC FREEPROCCACHE(use with caution in production as this clears all cached plans). - Check Permissions: Ensure the assembly is registered with the correct permission set (
SAFE,EXTERNAL_ACCESS, orUNSAFE). If the method attempts to access external resources but the assembly is markedSAFE, it may fail in ways that mimic a missing method. - Verify the DLL Version: Double-check that the .dll file you are deploying is actually the one you just compiled. Check the "Date Modified" timestamp on the file.
- Full Drop and Recreate: If
ALTER ASSEMBLYfails, drop the function, then drop the assembly usingDROP ASSEMBLY [AssemblyName];, and then recreate both from scratch.
Frequently Asked Questions
Can Error 228 be caused by a wrong DLL version?
Yes. If you deploy a version of the DLL where a method was renamed or a parameter was added, the existing SQL function will look for the old signature and fail because it no longer exists in the new assembly.
Do I need to restart SQL Server after updating an assembly?
No. Using ALTER ASSEMBLY updates the assembly in the database without requiring a service restart.
Why does it say the method doesn't exist when I can see it in the code?
This usually happens because the "signature" (the combination of name, return type, and parameter types) does not match exactly. Even a change from int to long makes it a different method in the eyes of the CLR.
Is this error related to "CLR enabled" settings?
No. If CLR were disabled, you would receive an error stating that the CLR is not enabled. Error 228 means CLR is working, but the specific bridge between SQL and the .NET code is broken.
Can I use a different assembly name for the same method?
Yes, but you must update the CREATE FUNCTION statement to point to the new assembly name and the correct method name within that assembly.