简体   繁体   English

尝试发布SQL CLR数据库的日期时间类型不匹配

[英]Datetime type mismatch trying to publish a SQL CLR database

I'm trying to stand up an old solution that's publishing a number of .net functions to a SQL Server database. 我正试图站出一个旧的解决方案,将一些.net函数发布到SQL Server数据库。 But the attempt to publish to a new database is failing on a function that manipulates dates. 但是,发布到新数据库的尝试在操作日期的函数上失败。

The function that's failing is: 失败的功能是:

[SqlFunction(TableDefinition="localtime datetime2", IsDeterministic=true, IsPrecise=true,
             DataAccess=DataAccessKind.None,
             SystemDataAccess=SystemDataAccessKind.None)]
public static DateTime ConvertFromUTC(DateTime utctime, string timezoneid)
{
    if (utctime.Kind == DateTimeKind.Unspecified)
        utctime = DateTime.SpecifyKind( utctime, DateTimeKind.Utc );

    utctime = utctime.ToUniversalTime();

    return TimeZoneInfo.ConvertTimeBySystemTimeZoneId( utctime, timezoneid );
}

The error message I get when attempting to publish is: 我尝试发布时收到的错误消息是:

Creating [dbo].[ConvertFromUTC]... 创建[dbo]。[ConvertFromUTC] ...

(268,1): SQL72014: .Net SqlClient Data Provider: (268,1):SQL72014:.Net SqlClient数据提供者:

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

(268,0): SQL72045: Script execution error (268,0):SQL72045:脚本执行错误

SQL generated from the .net in an attempt to add the function: 从.net生成的SQL,试图添加该函数:

CREATE FUNCTION [dbo].[ConvertFromUTC]
    (@utctime DATETIME, @timezoneid NVARCHAR (MAX))
RETURNS TABLE ([localtime] DATETIME2 (7) NULL)
AS EXTERNAL NAME [database].[IntelligentTutor.Database.Functions].[ConvertFromUTC]

SQL definition for the version of the function in the existing database (which confirms that @MattJohnson was right about how it needs fixed): 现有数据库中函数版本的SQL定义(确认@MattJohnson对于如何修复它是正确的):

CREATE FUNCTION [dbo].[ConvertFromUTC]
    (@utctime [datetime], @timezoneid [nvarchar](4000))
RETURNS [datetime] WITH EXECUTE AS CALLER
AS EXTERNAL NAME [database].[IntelligentTutor.Database.Functions].[ConvertFromUTC]

The SQL function doesn't match the .NET method signature. SQL函数与.NET方法签名不匹配。 To make it match: 使它匹配:

  1. Change your the type of @utctime to DATETIME2 instead of DATETIME in the function definition. 在函数定义@utctime的类型更改为DATETIME2而不是DATETIME

  2. Change the return type to just RETURNS DATETIME2 instead of returning a table with a nullable datetime2 column. 将返回类型更改为RETURNS DATETIME2而不是返回具有可为null的datetime2列的表。

Also, note that if you are on SQL 2016 or later, or on Azure SQL DB, you don't need this function, as you can now use AT TIME ZONE instead. 另请注意,如果您使用的是SQL 2016或更高版本,或者在Azure SQL DB上,则不需要此功能,因为您现在可以使用AT TIME ZONE

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

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