Thursday, March 8, 2012

CLR initialization failed with hresult 0x80131022

I guess this may not strictly be a CLR integration question, because what I'm actually doing is using sp_OACreate in a T-SQL batch to call a COM object written in C#. Not as nice as using full CLR integration, but unfortunately the T-SQL must run without error on a SQL 2000 server before it is upgraded to 2005, and I preferred this option to using reams of dynamic SQL.

The first call to sp_OACreate fails with hRes 0x80131022. MSSQLServer also logs the following error to the app event log:

"Failed to initialize the Common Language Runtime (CLR) v2.0.50727 with HRESULT 0x80131022. You may fix the problem and try again later."

Please can anyone suggest why this is happening? I've tried repairing the .NET framework (v2) installation, and reinstalled SQL Server Express. I've double checked both CLR integration and OLE Automation are allowed in the Surface Area config tool for SQL Express. I've tried running SQL Express using both LocalSystem and the administrator account on the machine. I've even checked there is plenty of free memory (including virtual memory). Nothing has helped though.

Thanks in advance for any help or suggestions you can provide,

Rob

Trying to load the CLR from SP_OA procedures is not supported and we block it in 2005, as described in the following kb: http://support.microsoft.com/kb/322884

It looks like you'll need to use different wrapper code on 2000 and 2005 to create your clr-based routines.

|||

Thanks for the response, but I notice the article clearly states it applies to 7.0 and 2000 only, not 2005 as you've suggested in your answer? Assuming CLR can be loaded when calling sp_OA procs on a 2005 installation, let me explain my question a bit more clearly...

Due to the lack of SQL Server Agent on an Express installation, my (pseudo) T-SQL code is structured as thus:

if @.@.version <= 8.x

sp_start_job 'SQL 2000 Job'

else

sp_OACreate 'JobReplacement.ForSQLExpress'

This way, when running on SQL 2000, the T-SQL follows the first branch, starting a SQL job. As soon as the DB engine has been upgraded, the T-SQL follows the alternative path, using the sp_OA procedures to call onto a C# coded COM object (which basically does the same things the job did on SQL 2000). Thus the T-SQL is effectively version neutral, and allows the engine to be upgraded at an arbitrary time without any further changes to the T-SQL code.

Therefore, as you can see, the sp_OA procedures are only called when running on SQL 2005 (Express). If you are telling me (contrary to what is stated in the KB article) that this is not possible under 2005, I will indeed re-think my approach. Otherwise, perhaps someone can help me further?

Thanks again,

Rob

|||

The KB doesn't obviously state the Sql Server 2005 limitation as its primary purpose is to state that using SP_OA to load the clr on Sql 2000 or earlier is unsupported. The relevent section to 2005 is in this line:

"Later versions of SQL Server will host Common Language Runtime (CLR) and will support procedures, functions, triggers, types, and aggregates that are written in CLR langauges. In these later versions, you cannot load CLR by using extended stored proceduress or sp_OA stored procedures."

So the KB article does not officially apply to SQL Server 2005 because while using SP_OA to load the CLR was unsupported in SQL Server 2000, it is impossible in SQL Server 2005.

My suggestion would be to use the built-in clr integration support on SQL Server 2005, however the only way to do this in a T-SQL neutral fashion would be with a lot of dynamic sql to enable clr, create assembly, create procedure, etc.|||Ok, thanks for your help.

No comments:

Post a Comment