Why This Error Happens
Error 6502 occurs when SQL Server attempts to load a CLR assembly (via CREATE ASSEMBLY or ALTER ASSEMBLY) but cannot physically access the DLL file specified in the path. This is typically caused by the SQL Server service account lacking NTFS read permissions on the file or folder, the file being moved/deleted after the statement was issued, or the path being an inaccessible UNC/network share that the service account cannot authenticate against. It can also occur if the file is locked by another process, is corrupted, or if Windows Defender/antivirus software is actively blocking access to the .dll file.
Step-by-Step Resolution Guide
Step 1: Verify the File Path and Existence
- Confirm the exact path referenced in the error message exists on disk (check for typos, trailing spaces, or incorrect drive letters).
- Open File Explorer or run
dir "C:\Path\To\Assembly.dll"in Command Prompt to confirm the file is present. - If the path is a UNC share (e.g.,
\\Server\Share\Assembly.dll), ping the host and confirm share visibility from the SQL Server machine itself, not just your workstation.
Step 2: Grant NTFS Permissions to the SQL Server Service Account
- Open Services.msc on Windows Server 2025 and note the exact logon account used by the SQL Server (MSSQLSERVER) service.
- Right-click the folder containing the DLL → Properties → Security tab → Edit → Add.
- Grant the SQL Server service account (e.g.,
NT Service\MSSQLSERVERor a domain service account) Read & Execute permissions. - Apply changes and restart the SQL Server service via
net stop MSSQLSERVER && net start MSSQLSERVERif permissions were modified while the service was running.
Step 3: Check for File Locks or Antivirus Interference
- Use Resource Monitor (resmon.exe) → CPU tab → Associated Handles to search for the DLL filename and identify any process locking it.
- Temporarily disable Windows Defender real-time protection or add an exclusion for the assembly folder:
Add-MpPreference -ExclusionPath "C:\Path\To\Assembly.dll"(run in elevated PowerShell).- Re-attempt the
CREATE ASSEMBLYstatement after clearing the lock or exclusion.
Step 4: Validate the Assembly File Integrity
- Confirm the DLL is not corrupted by copying it to another machine and inspecting it with
dumpbin /headers Assembly.dllor re-deploying a fresh copy from source control/build output. - If the file was transferred via FTP or email, re-transfer using binary mode to avoid corruption.
Step 5: Re-run the CREATE/ALTER ASSEMBLY Statement
- Execute the original T-SQL command again from SQL Server Management Studio (SSMS) using an account with
CREATE ASSEMBLYpermissions. - Example:
CREATE ASSEMBLY MyAssembly FROM 'C:\Path\To\Assembly.dll' WITH PERMISSION_SET = SAFE;
Alternative or Verification Steps
- After applying fixes, verify success by querying
SELECT * FROM sys.assemblies WHERE name = 'MyAssembly';to confirm the assembly loaded without error. - If permissions-based fixes fail, use the
FROM (varbinary)syntax instead of a file path—read the DLL into a variable usingOPENROWSET(BULK ... , SINGLE_BLOB)and pass it directly toCREATE ASSEMBLY, bypassing file-system access issues entirely.