We have developed an assembly for SQL Server 2008 R2.
The assembly has been working for a week. The managed stored proc inside the assembly was working fine for the
I have the same error and noticed what is wrong : Have a look at your assembly ID ! It is 65536 - mine is 65538
It seems the assembly ID is coded on a 16bits integer. So, "server may be running out of resources" takes a logical sense.
Microsoft bug, in my opinion. If you have find a better way than reboot or restart the service, please let me know! :)
We saw this error when trying to update spatial columns on a new server which was running SQL Server 2017.
Credit to the head of IT at our client company who found out that:
Sql 2017 introduced new trust rules for CLR (SQL 2012 wasn't a problem)... Even 'safe' CLR has to have been signed (which this dll isn't) or you have to force the trust as below:
DECLARE @clrName nvarchar(4000) = 'sqlspatialtools, version=0.0.0.0, culture=neutral, publickeytoken=null, processorarchitecture=msil'
DECLARE @asmBin varbinary(max) = 'PUT THE BINARY STRING HERE (GET FROM SCRIPTING CREATE TO FOR THE EXISTING ASSEMBLY'
DECLARE @hash varbinary(64);
SELECT @hash = HASHBYTES('SHA2_512', @asmBin);
EXEC sys.sp_add_trusted_assembly @hash = @hash, @description = @clrName;
This fixed the issue for us.
Assemblies with EXTERNAL_ACCESS are, through some convoluted path, falling under the EXECUTE AS path. The problem appears when the 'dbo' cannot be mapped to a valid login. dbo's login is the login with the SID the owner_sid
value in sys.databases. Unless an AUTHORIZATION clause was used in CREATE DATABASE the owner_sid is the login sid of the principal issuing the CREATE DATABASE statement. Most times this is the Windows SID of the user logged in and issuing the CREATE DATABASE. With this knowledge in hand one can easily envision the problems that may arise:
MachineA\user
or DomainA\user
) then the database was copied to machine B (via backup/restore or via file copy). The owner_sid is preserved by file copy as well as by backup/restore, this on machine B the owner_sid is invalid. Everything requiring EXECUTE As fails, including loading assemblies from the database.All these issues can be diagnosed by simply running: EXECUTE AS USER = 'dbo';
in the context of the problem db. It it fails with an error then the cause of your assembly load problems is the EXECUTE AS context of dbo
.
The solution is trivial, simply force the owner_sid
to a valid login. sa
is the usually the best candidate:
ALTER AUTHORIZATION ON DATABASE::[<dbanme>] TO sa;
The funny thing is that the database may seem to be perfectly healthy; tables are available and you can run selects, updates, deletes, create and drop tables etc. Only certain components require EXECUTE AS
:
EXECUTE AS
in T-SQL codeThe latter is the most often seen culprit, as applications relying on SqlDependency all of a sudden seem to stop working, or have random problems. This article explains how SqlDependency
ultimately depends on EXECUTE AS: The Mysterious Notification
Problem in my case was that DB restore was executed with Windows Authentication on SQL Server! Droping DB, loging in with sa, restoring DB again and setting TRUSTWORTHY ON, solved my problem!
This is weird. I had the same issue but I confirmed that the dbo account was valid via running a quick query: SELECT 'TEST' AS Test EXECUTE AS USER = 'dbo' I also verified that Trustworthy was set to True.
What fixed it for my box was changing the "assembly owner" from dbo to my own user and afterwards back to dbo.
What namespaces are you referencing in the assembly? SQL Server only officially supports a handful of the references that .net has available.
I've seen the exact same issue when referencing System.DirectoryServices (unsupported). We had a clr table valued function that would work great for a week or so and then, all of the sudden, would error. A redeploy or recycle of the service would temporarily fix the issue.
Make sure all of your namespace references are supported. Otherwise, you can potentially bring down the database.