问题
I'm trying to write a SQLCLR function that will return a nullable boolean value. If I declare the function like this: public static SqlBoolean? IsTimeZoneDST(SqlString timeZone)
then I get an error when attempting to use the assembly saying the types for the return value do not match. Is there something I am missing or is it not possible to return a nullable boolean in this instance?
回答1:
All of the .NET Sql*
types have a .Null
field (static property) that creates a new instance of what will be considered a NULL
value for that datatype within T-SQL.
Also, there are several other common properties and methods of all of the Sql*
types:
.IsNull
to test if the value isNULL
as far as T-SQL is concerned.Value
will return the equivalent .NET datatype, such as aString
forSqlString
, orint
/Int32
forSqlInt32
, etc.
The only time that you should need to use a .NET nullable type, I believe, is when using the T-SQL DATETIME2
datatype, which maps to either DateTime
or DateTime?
.
回答2:
Thanks juharr. I knew I'd be missing something simple. The answer in case anyone else needs it is to change the declaration to: public static SqlBoolean IsTimeZoneDST(SqlString timeZone)
and then when you need it return SqlBoolean.Null
来源:https://stackoverflow.com/questions/42906337/sql-clr-function-to-return-nullable-boolean