Attach database loses CLR

Hi

When I deploy a database (mdf and ldf) and attch the database I get errors when my stored procedures attempt to call CLR stored procs.

Error regards database trust and assembly safety etc. This works on my development system/database so it's setup correclty there.

Clearly I'm missing some additional setup when attaching the database to another server instance. Can't figure it out though. I'm marking the assembly UNSAFE (it requires network access) and the database as trustworthy when attaching but the error persists. Also making sure the server instance has CLR enabled.

What am I missing? Any ideas?

[644 byte] By [Dreedle] at [2007-12-25]
# 1

hi,

AFAIK, you only need to enable CLR integration (SQL Server Area Configuration for features) and obviously enable security settings for the db as

ALTER DATABASE [theDB] SET TRUSTWORTHY ON; GO CREATE ASSEMBLY [assemblyName] AUTHORIZATION owner_name FROM 'c:\..\full_path_to_the_dll.Dll' WITH PERMISSION_SET = EXTERNAL_ACCESS; GO CREATE PROCEDURE [schema].[procedure] @param .... AS EXTERNAL NAME [...].[...].[...]; GO

as you already did... are you dealing a "traditional" scenario or User Instances?

regards

AndreaMontanari at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 2

Andrea,

Traditional, user instances don't suit the application.

When attaching the DB to another server instance I set the database trust (as per your example) but I don't recreate the assembly or the CLR stored procs - aren't they already in the DB?

I'm thinking there must be something I have to set up in master DB because the server instance I'm attaching to has no previous knowledge of the CLR requirements of my DB? Shot in the dark.

Regards

Allan

Dreedle at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 3

I'm going to move this to the CLR forum, your question is not unique to Express, so they'll have more info over there.

Mike

MikeWachal-MSFT at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 4
What exact error message are you seeing upon using CLR in your newly attached DB?
MatHenaire at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 5
If errors are related to security, you may need to re-issue an

ALTER DATABASE databasename SET TRUSTWORTHY ON

on the newly attached db...

carlop at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 6

Mat,

The error is:

"An error occurred in the Microsoft .NET Framework while trying to load assembly id 66533. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error: "

followed by a stack trace. The stack trace just shows assembly load failure. Exception hresult is 0x80FC0B59 - which does not seem to be a standard hresult.

The database has 3 assemblies. One is EXTERNAL_ACCESS, one is UNSAFE and the third is SAFE.

Assembly 66533 is the external access assembly, which uses the unsafe assembly. The unsafe assembly is never called directly. The safe assembly is fine in all cases.

The error occurs when the MDF & LDF files of the database are installed on the the server and attached to the local SQL Server instance and the stored procs exposed by the assembly are called.

When attaching the database I set it trustworthy on. Makes no difference. Granting external access and unsafe to the login doesn't make any difference either.

Seems that attaching the database to another instance really screws up external access and unsafe assemblies. Detaching and attaching within the same server instance is fine. Which leads me to believe that there is an additional step required when attaching to another instance. I'm not enough of an sql server admin to figure out what though.

Dreedle at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 7

Hi Allan,

You have the problem exactly right - attaching the db to a different instance can cause the security check for creating external_access and unsafe assemblies. You can fix this by running sp_changedbowner on the database to associate the dbo with an logon that exists on the new server instance. See KB http://support.microsoft.com/default.aspx/kb/918040 for more information. (The error message for this case will be fixed in SP2 to provide more helpful information).

Steven

StevenHemingray-MSFT at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 8

Many thanks Steven, that got it. Everything working as it should.

Dreedle at 2007-10-8 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...

SQL Server

Site Classified