SQL CLR Function to return nullable boolean

て烟熏妆下的殇ゞ 提交于 2020-01-03 18:58:21

问题


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 is NULL as far as T-SQL is concerned
  • .Value will return the equivalent .NET datatype, such as a String for SqlString, or int / Int32 for SqlInt32, 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

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