I have a custom function in SQL Server (written by .NET) that clears text. I am wondering how to handle null input.
Here is the function in C #:
[Microsoft.SqlServer.Server.SqlFunction] public static SqlChars cleanEstActText(SqlChars input) { SqlChars cascadingSqlChar = removeNBSP(input); cascadingSqlChar = optimizeFontTags(cascadingSqlChar); return cascadingSqlChar; }
This is an error in SQL if the function receives 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)
Reading on SO and Google led me to the OnNullCall attribute, which looks promising.
From MSDN :
true if the method is called when the input argument values โโare null (Nothing in Visual Basic); false if the method returns null (Nothing in Visual Basic) when any of its input parameters is null (Nothing in Visual Basic).
It sounds exactly the way I want; if i get null just skip null. I'm not quite sure how to implement it, so I check the MSDN again (http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.server.sqlmethodattrib.aspx) and rewrite the first line of my function from
[Microsoft.SqlServer.Server.SqlFunction]
to
[Microsoft.SqlServer.Server.SqlMethod(OnNullCall = false, IsMutator = false, InvokeIfReceiverIsNull = false)]
If I do this, I get an error in SQL anytime I use it:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.cleanEstActText", or the name is ambiguous.
Am I implementing OnNullCall incorrectly? Should I do something else? Is there any good way to get my function to pass null through?