I'm trying to create a simple CLR User defined function using Visual Studio 2010 and SQL Server 2012. It builds just fine, but when I try to debug I get this error:
SqlClrDeploy:
Beginning deployment of assembly CCOMM_CLR.dll to server Titan : CBMAPP_REMAS
The following error might appear if you deploy a SQL CLR project that was built for a version of the .NET Framework that is incompatible with the target instance of SQL Server: "Deploy error SQL01268: CREATE ASSEMBLY for assembly failed because assembly failed verification". To resolve this issue, open the properties for the project, and change the .NET Framework version. C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\TeamData\Microsoft.Data.Schema.SqlClr.targets(96,5): Deploy error SQL01234: The database version is not supported.Build FAILED
So... How about build and deploy on the machine it self... Here is the code to install the assembly and the UDF
CREATE ASSEMBLY CCOMM_CLR
FROM 'E:\SQL\ASSEMBLIES\CCOMM_CLr.dll' WITH PERMISSION_SET = SAFE;
CREATE FUNCTION HelloXP(@Name nvarchar )
Returns nvarchar
As EXTERNAL NAME CCOMM_CLR.UserDefinedFunctions.HelloWorld;
and here is the entire UDF
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString HelloWorld(SqlString theName)
{
// Put your code here
return "Hello " + theName;
}
};
Installing of the assembly and the UDF works fine, but when I run it...
Select Master.dbo.HelloXP('Chris')
I get this error
Msg 6522, Level 16, State 2, Line 2
A .NET Framework error occurred during execution of user-defined routine or aggregate "HelloXP": System.Data.SqlServer.TruncationException: Trying to convert return value or output parameter of size 14 bytes to a T-SQL type with a smaller size limit of 2 bytes.
System.Data.SqlServer.TruncationException:
at System.Data.SqlServer.Internal.CXVariantBase.StringToWSTR(String pstrValue, Int64 cbMaxLength, Int32 iOffset, EPadding ePad)
What am I doing wrong? This seems fairly straight forward in the examples by MS so something is messed up. Yes CLR is enabled on the server. I'm already running extended stored procedures and they seemed a lot easier than the CLR.
Thanks, Chris