Types don't match for creating a CLR Stored Procedure

丶灬走出姿态 提交于 2021-02-20 07:02:58

问题


I have a method in an assembly that looks like this:

namespace MyNameSpace
{
  public class MyClass
  {
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void MyMethod(SqlChars myMessage)
    {
       // Stuff here 
    } 
  }
}

I compile that to a dll and load it into my SQL Server like this:

drop assembly MyAssembly
create assembly MyAssemblyfrom 'C:\Scripts\MyAssembly.dll'

That all works fine.

But now I try to run this:

CREATE PROCEDURE MySproc @message nvarchar(max)
AS EXTERNAL NAME MyAssembly.[MyNameSpace.MyClass].MyMethod

When I run that I get this Error:

CREATE PROCEDURE for "MySproc " failed because T-SQL and CLR types for parameter "@myMessage" do not match.

Any ideas how I can resolve this?

I have also Tried these method signatures:

  • public static void MyMethod(string myMessage)
  • public static void MyMethod([SqlFacet(MaxSize = -1)] string myMessage)

回答1:


Sql type nvarchar maps to CLR (.Net) type SqlString:

public static void MyMethod(SqlString myMessage)
{
    if (! myMessage.IsNull) 
    {
        String myMessageString = myMessage.ToString();
        // Do stuff
    }
}



回答2:


Here is a link showingg the mapping of CLR to SQL Server Data Types

http://msdn.microsoft.com/en-us/library/ms131092.aspx

In your case SQLChars maps to nvarchar



来源:https://stackoverflow.com/questions/9899197/types-dont-match-for-creating-a-clr-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!