SQL Server -- 使用 OnNullCall 处理 CLR 用户定义函数 (UDF) 中的空输入 [英] SQL Server -- Handling null input in CLR User-Defined Function (UDF) with OnNullCall

查看:89
本文介绍了SQL Server -- 使用 OnNullCall 处理 CLR 用户定义函数 (UDF) 中的空输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server(用 .NET 编写)中有一个用户定义的函数来清理文本.我想知道如何处理空输入.

I have a user-defined function in SQL Server (written in .NET) that cleans text. I'm wondering how to handle null input.

这是C#中的函数:

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlChars cleanEstActText(SqlChars input)
{
    SqlChars cascadingSqlChar = removeNBSP(input);
    cascadingSqlChar = optimizeFontTags(cascadingSqlChar);

    return cascadingSqlChar;
}

如果函数得到任何空数据,这是 SQL 中的错误:

This is the error in SQL if the function gets any null data:

A .NET Framework error occurred during execution of user-defined routine or aggregate "removeNBSP": 
System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
System.Data.SqlTypes.SqlNullValueException: 
   at System.Data.SqlTypes.SqlChars.get_Value()
   at UserDefinedFunctions.removeNBSP(SqlChars input)

阅读 SO 和 Google 使我想到了 OnNullCall 属性,它看起来很有希望.

Reading on SO and Google led me to the OnNullCall attribute, which looks promising.

来自 MSDN:

如果在方法调用中指定了 null(在 Visual Basic 中为 Nothing)输入参数时调用该方法,则为 true;如果该方法在其任何输入参数为 null(在 Visual Basic 中为 Nothing)时返回 null(在 Visual Basic 中为 Nothing)值,则为 false.

true if the method is called when null (Nothing in Visual Basic) input arguments are specified in the method invocation; false if the method returns a null (Nothing in Visual Basic) value when any of its input parameters are null (Nothing in Visual Basic).

听起来和我想要的完全一样;如果我得到空值,只需传递空值.我不太清楚如何实现它,所以我再次检查 MSDN(http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.server.sqlmethodattribute.aspx),并重写了第一行我的功能来自

Sounds exactly like what I want; if I get null, just pass null through. I'm not quite sure how to implement it, so I check MSDN again (http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.server.sqlmethodattribute.aspx), and rewrite the first line of my function from

[Microsoft.SqlServer.Server.SqlFunction]

[Microsoft.SqlServer.Server.SqlMethod(OnNullCall = false, IsMutator = false, InvokeIfReceiverIsNull = false)]

如果我这样做,每次使用 SQL 时都会出错:

If I do this, I get an error in SQL any time I use it:

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.cleanEstActText", or the name is ambiguous.

我是否错误地实施了 OnNullCall?我应该做点别的吗?真的有什么好方法可以让我的函数传递空值吗?

Am I implementing OnNullCall incorrectly? Should I be doing something else? Is there really any good way to make my function pass nulls through?

推荐答案

你可以试试这个

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlChars cleanEstActText(SqlChars input)
{

    if (input.IsNull) return null;

    SqlChars cascadingSqlChar = removeNBSP(input);
    cascadingSqlChar = optimizeFontTags(cascadingSqlChar);

    return cascadingSqlChar;
}

所有可空的 SqlData 类型都有一个 IsNull 属性.

All Nullable SqlData Types have an IsNull Propery.

谢谢哈里

这篇关于SQL Server -- 使用 OnNullCall 处理 CLR 用户定义函数 (UDF) 中的空输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆