简体   繁体   English

SQL Server datetime2 解析问题

[英]SQL Server datetime2 parse issue

When I run the following SQL at my local SQL Server instance:当我在本地 SQL Server 实例上运行以下 SQL 时:

DECLARE @t DATETIME2 = '2019-12-12 00:00:00'

I get the following error:我收到以下错误:

Unable to convert @t to a System.Data.SqlClient.SqlParameter object.无法将 @t 转换为 System.Data.SqlClient.SqlParameter 对象。 The specified literal cannot be converted to DateTime2(System.Data.SqlDbType), as it used an unsupported date/time format.指定的文字无法转换为 DateTime2(System.Data.SqlDbType),因为它使用了不受支持的日期/时间格式。 Use one of the supported date/time formats.使用一种受支持的日期/时间格式。 Literal value: 2019-12-12 00:00:00字面值:2019-12-12 00:00:00

The only way to fix it is to add 'T' between date and time修复它的唯一方法是在日期和时间之间添加“T”

DECLARE @t DATETIME2 = '2019-12-12T00:00:00'

At the same time, this script runs perfectly fine at my production server (without adding 'T').同时,这个脚本在我的生产服务器上运行得非常好(没有添加“T”)。

I tried to find the difference and don't see anything that could affect the behavior:我试图找到差异,但没有看到任何可能影响行为的东西:

  • Collation is the same at both servers - SQL_Latin1_General_CP1_CI_AS两台服务器的排序规则相同 - SQL_Latin1_General_CP1_CI_AS
  • Language is the same - English (United States)语言是一样的 -英语(美国)
  • Server version: LOCAL is Microsoft SQL Server 2017 Developer Edition (64-bit) v14.0.2027 RTM;服务器版本:LOCAL为Microsoft SQL Server 2017 Developer Edition (64-bit) v14.0.2027 RTM; REMOTE is Microsoft SQL Server 2017 Enterprise Edition: Core-based Licensing (64-bit) v14.0.1000 RTM REMOTE 是 Microsoft SQL Server 2017 Enterprise Edition: Core-based Licensing (64-bit) v14.0.1000 RTM
  • Host OS: LOCAL is Windows 10 Pro;主机操作系统:LOCAL 是 Windows 10 Pro; REMOTE is Windows Server 2016 Datacenter REMOTE 是 Windows Server 2016 数据中心

Can somebody explain, why I'm getting the error locally?有人可以解释一下,为什么我在本地收到错误消息?

This is a client-side error so it seems you are running the query from SSMS with the Enable Always Encrypted (column encryption) option for the connection (connection Options-->Always Encrypted) on the remote client but not the prod server.这是一个客户端错误,因此您似乎正在从 SSMS 运行查询,并为远程客户端上的连接(连接选项--> 始终加密)启用始终加密(列加密)选项,而不是生产服务器。 With the option enabled, the SSMS client parses literals in order to build parameters for AE.启用该选项后,SSMS 客户端会解析文字以便为 AE 构建参数。

Unlike '2019-12-12 00:00:00' , the ISO 8601 datetime format literal '2019-12-12T00:00:00' is unambiguous and can be reliably parsed regardless of the client regional settings.'2019-12-12 00:00:00'不同, ISO 8601 日期时间格式文字'2019-12-12T00:00:00'是明确的,无论客户端区域设置如何,都可以可靠地解析。

So your options are to either use an ISO 8601 datetime format or turn of the SSMS Enable AE option for the connection.因此,您的选择是使用 ISO 8601 日期时间格式或为连接启用 SSMS 启用 AE 选项。

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

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