Registering a SQLCLR function DLL into SQL Server when EXTERNAL_ACCESS is needed

Posted: (EET/GMT+2)

 

SQLCLR assemblies are .NET DLL files that must be registered to SQL Server. However, if you try to run the CREATE ASSEMBLY command and you need the EXTERNAL_ACCESS permission, you might run into the following error message:

Msg 10327, Level 14, State 1, Line 7
CREATE ASSEMBLY for assembly 'MySqlClrFunction' failed because assembly 'SqlClrTest' is
not authorized for PERMISSION_SET = EXTERNAL_ACCESS. The assembly is authorized when
either of the following is true: the database owner (DBO) has EXTERNAL ACCESS ASSEMBLY
permission and the database has the TRUSTWORTHY database property on; or the assembly
is signed with a certificate or an asymmetric key that has a corresponding login with
EXTERNAL ACCESS ASSEMBLY permission.

To solve this, you can grant more rights or create asymmetric keys. Since you cannot grant more rights to yourself, you first need to log in as another sysadmin, and then run these commands in the context of the Master database:

ALTER DATABASE MyDatabaseName SET TRUSTWORTHY ON;
GRANT EXTERNAL ACCESS ASSEMBLY TO [YourUserName];

Then, log back again with your own sysdba/dbo account, and execute a command similar to the following:

CREATE ASSEMBLY MySQLCLRTest FROM 'C:\PathToDll\MySqlClrFunctionTest.dll'   
WITH PERMISSION_SET = EXTERNAL_ACCESS;

Hope this helps!