資料庫設定:
CREATE TABLE [dbo].[LOG]
(
[LOAD_DATE] [datetime] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[PRODUCTS]
(
[PRODUCT_ID] [int] NULL,
[PRODUCT_NAME] [nchar](100) NULL,
[DATE_MODIFIED] [datetime] NULL
) ON [PRIMARY]
GO
INSERT INTO [LOG] (LOAD_DATE)
VALUES (GETDATE())
SELECT * FROM [LOG]
SSIS:
執行 SQL 任務:
SELECT ?=MAX([Load_Date])
FROM [LOG]
該任務有一個資料型別的輸出引數(比如 Param1)DT_DBTIMESTAMP。SSIS 變數資料型別是DateTime.
然后在隨后的資料流任務(比如 TASK2)中,我有一個 OLE DB 源 SQL 命令文本:
SELECT *
FROM CANDIDATE
WHERE CANDIDATE_TIMESTAMP>?
上述變數用作輸入引數。
在 SQL 探查器中,我可以看到缺少毫秒。那么如果 SSIS 變數不存盤毫秒部分呢?如何確保毫秒部分傳遞到查詢中?
uj5u.com熱心網友回復:
這是 OLE DB 提供程式問題。
OLE DB 提供程式(在我的情況下:SQL Server Native Client 11)將所有DateTime輸入引數轉換為DateTime2(0)SQL Server,即使您嘗試使用強制轉換函式強制資料型別,例如考慮以下 OLE DB 源中的 SQL 命令.
SELECT *
FROM Users
Where CreationDate > Cast(? as datetime2(3))
從下面的 SQL Profiler 螢屏截圖中,您可以看到 OLE DB 提供程式如何強制DateTime2(0)引數的資料型別。

將 DateTime 值從 OLE DB 資料型別轉換為資料庫引擎資料型別時,毫秒會被截斷。奇怪的是這兩種資料型別都支持小數秒(毫秒)
如果您想了解有關 SSIS 中不同資料型別系統的更多資訊,可以查看此答案中的“附加資訊”部分。
更多解釋
我將創建一個User::CurrentDate具有以下值的 SSIS DateTime 變數:10/10/2021 12:00:01.001.

在 OLE DB Source 中,我將使用以下陳述句:
SELECT *
FROM Users
Where CreationDate > Cast(? as datetime)
然后,我將選擇創建的變數作為輸入引數。

現在,如果我單擊預覽按鈕,則會引發以下例外:
標題:Microsoft Visual Studio
顯示預覽時出錯。
附加資訊:
提供的時間值的小數部分溢位了相應 SQL Server 引數或列的小數位數。增加 DBPARAMBINDINFO 中的 bScale 或列比例以更正此錯誤。(Microsoft SQL Server 本機客戶端 11.0)

如果我們點擊“顯示詳細資訊”按鈕,例外堆疊跟蹤顯示錯誤是在System.Data.OleDb.OleDbDataReader類上拋出的。這意味著 OLE DB 提供程式是導致問題的原因。

解決方法
You can use String data type to store the value within SSIS and use the CONVERT() function in the OLE DB Source SQL Command to convert it to DateTime within the SQL Server database engine.
Example
Use the following SQL Statement in the Execute SQL Task:
SELECT ? = CONVERT(VARCHAR(23), max([Load_Date]), 121) FROM [LOG]
And store the result within an Output parameter of type String. Then, within the OLE DB Source use the following SQL Command:
SELECT *
FROM CANDIDATE
WHERE CANDIDATE_TIMESTAMP > CONVERT(DATETIME, ?, 121)
References
- SSIS 2012 : work around for missing milliseconds in SSIS datetime variable Solution for incremental load using datetime column
Additional Information
SSIS Data types
For a better understanding, the different data types used in SSIS are worth mentioning. There are several data type systems used within the Integration services:
- The Database Engine data types (SQL Server, Oracle, ...)
- The SSIS variables data types
- The SSIS Pipeline buffer (Data Flow Task) data types
- The connection provider data types (OLE DB, ADO.NET, ODBC)
Each data type used at any level in SSIS may have a corresponding data type in another class. A great resource was provided by "Matija Lah" where most of the data types mappings are provided, besides useful information.
The following table shows the data types mapping at different levels of SSIS (Check the article I mentioned for more explanation):
| SQL Server | SSIS Variables | SSIS Pipeline Buffer | OLE DB | ADO.NET |
|---|---|---|---|---|
| bigint | Int64 | DT_I8 | LARGE_INTEGER | Int64 |
| binary | Object | DT_BYTES | n/a | Binary |
| bit | Boolean | DT_BOOL | VARIANT_BOOL | Boolean |
| char | String | DT_STR | VARCHAR | StringFixedLength |
| date | Object | DT_DBDATE | DBDATE | Date |
| datetime | DateTime | DT_DBTIMESTAMP | DATE | DateTime |
| datetime2 | Object | DT_DBTIMESTAMP2 | DBTIME2 | DateTime2 |
| datetimeoffset | Object | DT_DBTIMESTAMPOFFSET | DBTIMESTAMPOFFSET | DateTimeOffset |
| decimal | Object (< SQL 2012) Decimal (>= SQL 2012) | DT_NUMERIC | NUMERIC | Decimal |
| float | Double | DT_R8 | FLOAT | Double |
| image | Object | DT_IMAGE | n/a | Binary |
| int | Int32 | DT_I4 | LONG | Int32 |
| money | Object | DT_CY (OLE DB) DT_NUMERIC (ADO.NET) | CURRENCY | Currency |
| nchar | String | DT_WSTR | NVARCHAR | StringFixedLength |
| ntext | String | DT_NTEXT | n/a | String |
| numeric | Object (< SQL 2012) Decimal (>= SQL 2012) | DT_NUMERIC | NUMERIC | Decimal |
| nvarchar | String | DT_WSTR | NVARCHAR | String |
| nvarchar(max) | Object | DT_NTEXT | n/a | n/a |
| real | Single | DT_R4 | FLOAT, DOUBLE | Single |
| rowversion | Object | DT_BYTES | n/a | Binary |
| smalldatetime | DateTime | DT_DBTIMESTAMP | DATE | DateTime |
| smallint | Int16 | DT_I2 | SHORT | Int16 |
| smallmoney | Object | DT_CY (OLE DB) DT_NUMERIC (ADO.NET) | CURRENCY | Currency |
| sql_variant | Object | DT_WSTR (OLE DB) DT_NTEXT (ADO.NET) | Object | |
| table | Object | n/a | ||
| text | Object | DT_TEXT | n/a | n/a |
| time | Object | DT_DBTIME2 | DBTIME2 | Time |
| timestamp | Object | DT_BYTES | n/a | Binary |
| tinyint | Byte | DT_UI1 | BYTE | Byte |
| uniqueidentifier | String (OLE DB) Object (ADO.NET) | DT_GUID | GUID | Guid |
| varbinary | Object | DT_BYTES | n/a | Binary |
| varbinary(max) | Object | DT_IMAGE | n/a | Binary |
| varchar | String | DT_STR | VARCHAR | String |
| varchar(max) | Object | DT_TEXT | n/a | n/a |
| xml | Object | DT_NTEXT |
DateTime with Fractional Seconds (Milliseconds)
The following are the DateTime types that stores milliseconds:
1. SSIS Pipeline buffer data types
Based on the official documentation, on the SSIS Pipeline buffer level, two data types stores the date and time with fractional seconds:
- DT_DBTIMESTAMP: The fractional seconds have a maximum scale of 3 digits.
HH:mm:ss.fff - DT_DBTIMESTAMP2: The fractional seconds have a maximum scale of 3 digits.
HH:mm:ss.fffffff
2. SSIS Variables data types
On the SSIS variables level, the DateTime data type stores fractional seconds with a maximum scale of 3 digits HH:mm:ss.fff
3. SQL Server data types
In the SQL Server database engine, the following data types support fractional seconds:
- DateTime: The fractional seconds have a maximum scale of 3 digits.
HH:mm:ss.fff - DateTime2: The fractional seconds have a maximum scale of 3 digits.
HH:mm:ss.fffffff
4. OLE DB Connection provider
In the OLE DB provider, the following data types support fractional seconds:
- DBTIMESTAMP:小數秒的最大刻度為 3 位。
HH:mm:ss.fff - DBTIME2:小數秒的最大刻度為 3 位。
HH:mm:ss.fffffff
uj5u.com熱心網友回復:
DT_DBTIMESTAMP2如果需要毫秒部分,可以使用資料型別。IT 是映射到Datetime2SQL Server 中的資料型別的那個。Besdies,請確保您使用的是DateTime2資料型別而不是DateTimeSQL Server 表中的資料型別。
查看以下帖子了解更多資訊:DT_Date | DT_DBDate | DT_DBTime | DT_DBTime2 | DT_DBTimeStamp | DT_DBTImeStamp2
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/399210.html
標籤:sql sql-server 约会时间 姐姐 等
下一篇:將時間戳轉換為特定時區的日期時間
