SQLCLR和DATETIME2 [英] SQLCLR and DateTime2

查看:238
本文介绍了SQLCLR和DATETIME2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SQL Server 2008,Visual Studio 2005中,.NET 2.0 SP2(具有新的SQL Server 2008数据类型的支持)。

Using SQL Server 2008, Visual Studio 2005, .net 2.0 with SP2 (has support for new SQL Server 2008 data types).

我试图写一个SQLCLR函数,它接受一个DATETIME2作为输入,并返回另一个DATETIME2。例如

I'm trying to write an SQLCLR function that takes a DateTime2 as input and returns another DateTime2. e.g. :

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);
        }
    }
}

现在,上面的编译罚款。我想这些SqlDateTimes映射到SQL Server的DATETIME2,所以我尝试运行这个T-SQL:

Now, the above compiles fine. I want these SqlDateTimes to map to SQL Server's DateTime2, so I try to run this T-SQL :

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

这提供了以下错误:

消息6551,级别16,状态2,过程   f_UTCToLocalDT,第1行CREATE FUNCTION   对于f_UTCToLocalDT失败的原因   T-SQL和CLR类型返回值   不匹配。

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.

使用DATETIME(而不是DATETIME2)工作正常。但我宁愿使用DATETIME2以支持增加precision。我究竟做错了什么,或者是DATETIME2未(完全)支持SQLCLR?

Using DATETIME (instead of DATETIME2) works fine. But I'd rather use DATETIME2 to support the increased precision. What am I doing something wrong, or is DateTime2 not (fully) supported by SQLCLR ?

推荐答案

您需要更改日期时间类型在你的函数方法的签名。 SQLDATETIME映射到数据库上的日期时间。

You need to change the DateTime types in the signature of your Function Method. SQLDateTime maps to a DateTime on the database.

System.DateTime的是更多的precise和的可以的映射到DATETIME2(但默认情况下,它会在部署脚本下降作为一个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).

[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

运行你的函数现在应该给你更多的precise输出。

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

这篇关于SQLCLR和DATETIME2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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