简体   繁体   English

SQLCLR和DateTime2

[英]SQLCLR and DateTime2

Using SQL Server 2008, Visual Studio 2005, .net 2.0 with SP2 (has support for new SQL Server 2008 data types). 使用SQL Server 2008,Visual Studio 2005,.net 2.0 with SP2(支持新的SQL Server 2008数据类型)。

I'm trying to write an SQLCLR function that takes a DateTime2 as input and returns another DateTime2. 我正在尝试编写一个SQLCLR函数,它将DateTime2作为输入并返回另一个DateTime2。 eg : 例如:

using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

namespace MyCompany.SQLCLR
{
    public class DateTimeHelpCLR
    {
        [SqlFunction(DataAccess = DataAccessKind.None)]
        public static SqlDateTime UTCToLocalDT(SqlDateTime val)
        {
            if (val.IsNull)
                return SqlDateTime.Null;

            TimeZone tz = System.TimeZone.CurrentTimeZone;
            DateTime res = tz.ToLocalTime(val.Value);

            return new SqlDateTime(res);
        }
    }
}

Now, the above compiles fine. 现在,上面的编译很好。 I want these SqlDateTimes to map to SQL Server's DateTime2, so I try to run this T-SQL : 我希望这些SqlDateTimes映射到SQL Server的DateTime2,所以我尝试运行这个T-SQL:

CREATE function hubg.f_UTCToLocalDT
(
    @dt DATETIME2
)
returns DATETIME2
AS
EXTERNAL NAME [SQLCLR].[MyCompany.SQLCLR.DateTimeHelpCLR].UTCToLocalDT
GO

This gives the following error : 这会出现以下错误:

Msg 6551, Level 16, State 2, Procedure f_UTCToLocalDT, Line 1 CREATE FUNCTION for "f_UTCToLocalDT" failed because T-SQL and CLR types for return value do not match. Msg 6551,级别16,状态2,过程f_UTCToLocalDT,第1行“f_UTCToLocalDT”的CREATE FUNCTION失败,因为返回值的T-SQL和CLR类型不匹配。

Using DATETIME (instead of DATETIME2) works fine. 使用DATETIME(而不是DATETIME2)工作正常。 But I'd rather use DATETIME2 to support the increased precision. 但我宁愿使用DATETIME2来支持提高的精度。 What am I doing something wrong, or is DateTime2 not (fully) supported by SQLCLR ? 我做错了什么,或者SQLCLR没有(完全)支持DateTime2?

You need to change the DateTime types in the signature of your Function Method. 您需要在函数方法的签名中更改DateTime类型。 SQLDateTime maps to a DateTime on the database. SQLDateTime映射到数据库上的DateTime。

System.DateTime is more precise and can be mapped to DateTime2 (but by default, it'll be dropped as a DateTime in the deploy script). System.DateTime更精确, 可以映射到DateTime2(但默认情况下,它将在部署脚本中作为DateTime删除)。

[SqlFunction(DataAccess = DataAccessKind.None)]
//OLD Signature public static SqlDateTime UTCToLocalDT(SqlDateTime val) 
public static DateTime UTCToLocalDT(DateTime val) {
   ...
}

Then you can tweak your deploy script to read. 然后,您可以调整部署脚本以进行阅读。

CREATE FUNCTION [UTCToLocalDT]
(
    @dt [datetime2]
)
RETURNS [datetime2]
AS
    EXTERNAL NAME [SQLCLR].[MyCompany.SQLCLR.DateTimeHelpCLR].UTCToLocalDT
GO

Running your function should now give you more precise output. 运行您的功能现在应该为您提供更精确的输出。

DECLARE @input DateTime2, @output DateTime2
SET @input = '2010-04-12 09:53:44.48123456'
SET @output = YourDatabase.dbo.[UTCToLocalDT](@input)
SELECT @input, @output

Note that using "DateTime?" 请注意使用“DateTime?” still always gives build errors (even in VS 2013 with sql 2012), though apparently the result is usable if one selects "build, deploy" and then uses the files in the obj folder, editing the generated.sql file in the sql query window (to use DateTime2 as parameter) before executing to add it to Sql Server. 仍然总是给出构建错误(即使在使用sql 2012的VS 2013中),但显然结果是可用的,如果选择“构建,部署”然后使用obj文件夹中的文件,在sql查询窗口中编辑generated.sql文件(在执行时将DateTime2用作参数)将其添加到Sql Server。
The build error is "SQL46010: Incorrect syntax near )." 生成错误是“SQL46010:语法不正确”。“ in \\obj\\Debug\\YourPrjName.generated.sql 在\\ obj \\ Debug \\ YourPrjName.generated.sql中

(Would post above as comment if I could.) (如果可以的话,会在上面发表评论。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM