简体   繁体   English

添加新的 CLR 用户定义函数时出现“找不到类型 nvarchar(MAX)”

[英]Getting “Cannot find the type nvarchar(MAX)” when adding new CLR user defined function

I have created a new function in an assembly which has been successfully added as a CLR Assembly to my SQL server.我在程序集中创建了一个新函数,该函数已作为 CLR 程序集成功添加到我的 SQL 服务器。 Now I am trying to create a SQL user defined function to map to the new method in that assembly.现在我正在尝试创建一个 SQL 用户定义函数来映射到该程序集中的新方法。 I have other methods in the assembly which have been mapped successfully.我在程序集中有其他方法已成功映射。

Here is my function这是我的功能

CREATE FUNCTION [dbo].[FromCamelCase](@value [nvarchar(MAX)])
RETURNS [nvarchar(MAX)] WITH EXECUTE AS CALLER
AS 
EXTERNAL NAME [Tools_CLR].[UserDefinedFunctions].[FromCamelCase]
GO

On execute, I am getting this error.执行时,我收到此错误。

Msg 15151, Level 16, State 1, Procedure FromCamelCase, Line 2
Cannot find the type 'nvarchar(MAX)', because it does not exist or you do not 
have permission.

I have tried changing the type from nvarchar to just varchar .我尝试将类型从nvarchar更改为varchar I have also tried changing the size from MAX to 4000 and also just 50.我还尝试将大小从MAX更改为 4000,也仅更改为 50。

Here is my C# method, for reference.这是我的 C# 方法,供参考。

[Microsoft.SqlServer.Server.SqlFunction]
public static string FromCamelCase(string val)
{
    if (val == null) return string.Empty;
    
    val = val.Replace("_", "");
    StringBuilder sb = new StringBuilder(val.Length + 10);
    bool first = true;
    char lastChar = '\0';
    
    foreach (char ch in val)
    {
        if (!first && (char.IsUpper(ch) || char.IsDigit(ch) && !char.IsDigit(lastChar)))
            sb.Append(' ');
    
        sb.Append(ch);
        first = false;
        lastChar = ch;
    }
    
    return sb.ToString();
}

Remove the brackets from around the data type name: RETURNS NVARCHAR(MAX) .删除数据类型名称周围的括号: RETURNS NVARCHAR(MAX) The double brackets mean it's interpreted as a user-defined data type called "NVARCHAR(MAX)" (one word) and quoted because of the parentheses in the name.双括号表示它被解释为一种用户定义的数据类型,称为“NVARCHAR(MAX)”(一个字)并由于名称中的括号而被引用

If you want to keep brackets for whatever reason, you can do that .如果您想出于任何原因保留括号,您可以这样做 Move the closing bracket to exclude the (MAX) like so:移动右括号以排除(MAX)如下所示:

RETURNS [NVARCHAR](MAX)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CLR用户定义的功能安全异常 - CLR User-Defined function security exception 如何在 CLR UDF 中返回 nvarchar(max)? - How to return an nvarchar(max) in a CLR UDF? 操作数类型冲突:nvarchar与用户定义的表类型不兼容 - Operand type clash: nvarchar is incompatible with user-defined table type Linq - 从 SQL 服务器中的 nvarchar 类型列中查找最大值 - Linq - Find max value from a nvarchar type column in SQL Server CLR SQL用户定义的类型将ID传递给存储过程的列表 - CLR SQL User Defined type pass list of ID to stored procedure C# CLR 用户定义 Function SqlBytes - InvalidOperationException - C# CLR User Defined Function SqlBytes - InvalidOperationException SQL CLR用户定义函数中的C#支持程度? - Degree of C# support in SQL CLR user-defined function? 如何注册CLR用户定义函数以在linq查询中使用? - How to register CLR User Defined Function for use in linq query? 无法在错误的varbinary(max)中插入空值:不允许从数据类型nvarchar隐式转换为varbinary(max) - Cannot insert null value in varbinary(max) with error : implicit conversion from data type nvarchar to varbinary(max) is not allowed 将nvarchar(MAX)列添加到我的审核表中 - Adding a nvarchar(MAX) Column to my audit table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM