jss-тип mssql-jdbc datetime преобразуется в datetime2 в совместимом SQLServer 2014 и SQLServer2016 - PullRequest
2 голосов
/ 01 апреля 2019

Мы используем SQLServer 2012-2016.Тип данных DATETIME является частью ключа во многих таблицах.когда мы выполняем запрос с помощью jdbcTemplate.query(query, queryParams, resultSetExtractor) Отправляется SQLServer:

sp_executesql N'SELECT * FROM [schema_example].[dbo].[tbl1] 
WHERE  (dtContactGMTStartTime>@P0 OR @P1 is NULL) 
 ORDER BY dtContactGMTStartTime ASC',
N'@P0 datetime2,@P1 datetime2','2014-01-04 03:10:49.9330000','2014-01-04 03:10:49.9330000'

Тип данных параметра - DATETIME2, хотя тип данных в базе данных - DATETIME.см. задокументированная проблема в github .Кроме того, SQLServer изменил свое поведение, см. Ссылку Улучшения SQLServer в 2016 году см. Ниже пример различий между уровнями совместимости Объединение двух вышеупомянутых проблем приводит к тому, что операторы SQL получают разные строки для одного и того жеданные в разных версиях SQLServer.

Существует ли обходной путь, отличный от изменения типа данных в базе данных на DATETIME2 (3)?

пример различий между уровнями совместимости

select '20020202 02:02:02.000' AS String, CONVERT(datetime, '20020202 02:02:02.000') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.000')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.000')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.000') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.000')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.001' AS String, CONVERT(datetime, '20020202 02:02:02.001') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.001')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.001')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.001') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.001')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.002' AS String, CONVERT(datetime, '20020202 02:02:02.002') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.002')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.002')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.002') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.002')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.003' AS String, CONVERT(datetime, '20020202 02:02:02.003') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.003')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.003')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.003') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.003')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.004' AS String, CONVERT(datetime, '20020202 02:02:02.004') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.004')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.004')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.004') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.004')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.005' AS String, CONVERT(datetime, '20020202 02:02:02.005') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.005')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.005')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.005') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.005')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.006' AS String, CONVERT(datetime, '20020202 02:02:02.006') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.006')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.006')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.006') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.006')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.007' AS String, CONVERT(datetime, '20020202 02:02:02.007') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.007')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.007')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.007') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.007')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.008' AS String, CONVERT(datetime, '20020202 02:02:02.008') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.008')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.008')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.008') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.008')) AS [Datetime2_3ToDatetime2] union all
select '20020202 02:02:02.009' AS String, CONVERT(datetime, '20020202 02:02:02.009') AS [StringToDatetime], CONVERT(datetime2(3),  CONVERT(datetime, '20020202 02:02:02.009')) AS [DatetimeToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime, '20020202 02:02:02.009')) AS [DatetimeToDatetime2], CONVERT(datetime2(3), '20020202 02:02:02.009') AS [StringToDatetime2_3], CONVERT(datetime2,  CONVERT(datetime2(3), '20020202 02:02:02.009')) AS [Datetime2_3ToDatetime2]

SET COMPATIBILITY_LEVEL = 120
String                                StringToDatetime                     DatetimeToDatetime2_3               DatetimeToDatetime2                    StringToDatetime2_3                 Datetime2_3ToDatetime2
---------------------                 -----------------------              ---------------------------         ---------------------------            ---------------------------         ---------------------------
20020202 02:02:02.000                 2002-02-02 02:02:02.000              2002-02-02 02:02:02.000             2002-02-02 02:02:02.0000000            2002-02-02 02:02:02.000             2002-02-02 02:02:02.0000000
20020202 02:02:02.001                 2002-02-02 02:02:02.000              2002-02-02 02:02:02.000             2002-02-02 02:02:02.0000000            2002-02-02 02:02:02.001             2002-02-02 02:02:02.0010000
20020202 02:02:02.002                 2002-02-02 02:02:02.003              2002-02-02 02:02:02.003             **2002-02-02 02:02:02.0030000**            2002-02-02 02:02:02.002             2002-02-02 02:02:02.0020000
20020202 02:02:02.003                 2002-02-02 02:02:02.003              2002-02-02 02:02:02.003             **2002-02-02 02:02:02.0030000**            2002-02-02 02:02:02.003             2002-02-02 02:02:02.0030000
20020202 02:02:02.004                 2002-02-02 02:02:02.003              2002-02-02 02:02:02.003             **2002-02-02 02:02:02.0030000**            2002-02-02 02:02:02.004             2002-02-02 02:02:02.0040000
20020202 02:02:02.005                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0070000**            2002-02-02 02:02:02.005             2002-02-02 02:02:02.0050000
20020202 02:02:02.006                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0070000**            2002-02-02 02:02:02.006             2002-02-02 02:02:02.0060000
20020202 02:02:02.007                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0070000**            2002-02-02 02:02:02.007             2002-02-02 02:02:02.0070000
20020202 02:02:02.008                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             2**002-02-02 02:02:02.0070000**            2002-02-02 02:02:02.008             2002-02-02 02:02:02.0080000
20020202 02:02:02.009                 2002-02-02 02:02:02.010              2002-02-02 02:02:02.010             2002-02-02 02:02:02.0100000            2002-02-02 02:02:02.009             2002-02-02 02:02:02.0090000

SET COMPATIBILITY_LEVEL = 130                                                                                                            
String                                StringToDatetime                     DatetimeToDatetime2_3               DatetimeToDatetime2                    StringToDatetime2_3                 Datetime2_3ToDatetime2
---------------------                 -----------------------              ---------------------------         ---------------------------           ---------------------------          ---------------------------
20020202 02:02:02.000                 2002-02-02 02:02:02.000              2002-02-02 02:02:02.000             2002-02-02 02:02:02.0000000            2002-02-02 02:02:02.000             2002-02-02 02:02:02.0000000
20020202 02:02:02.001                 2002-02-02 02:02:02.000              2002-02-02 02:02:02.000             2002-02-02 02:02:02.0000000            2002-02-02 02:02:02.001             2002-02-02 02:02:02.0010000
20020202 02:02:02.002                 2002-02-02 02:02:02.003              2002-02-02 02:02:02.003             **2002-02-02 02:02:02.0033333**            2002-02-02 02:02:02.002             2002-02-02 02:02:02.0020000
20020202 02:02:02.003                 2002-02-02 02:02:02.003              2002-02-02 02:02:02.003             **2002-02-02 02:02:02.0033333**            2002-02-02 02:02:02.003             2002-02-02 02:02:02.0030000
20020202 02:02:02.004                 2002-02-02 02:02:02.003              2002-02-02 02:02:02.003             **2002-02-02 02:02:02.0033333**            2002-02-02 02:02:02.004             2002-02-02 02:02:02.0040000
20020202 02:02:02.005                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0066667**            2002-02-02 02:02:02.005             2002-02-02 02:02:02.0050000
20020202 02:02:02.006                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0066667**            2002-02-02 02:02:02.006             2002-02-02 02:02:02.0060000
20020202 02:02:02.007                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0066667**            2002-02-02 02:02:02.007             2002-02-02 02:02:02.0070000
20020202 02:02:02.008                 2002-02-02 02:02:02.007              2002-02-02 02:02:02.007             **2002-02-02 02:02:02.0066667**            2002-02-02 02:02:02.008             2002-02-02 02:02:02.0080000
20020202 02:02:02.009                 2002-02-02 02:02:02.010              2002-02-02 02:02:02.010             2002-02-02 02:02:02.0100000            2002-02-02 02:02:02.009             2002-02-02 02:02:02.0090000
...