SQL Server SSIS Unsupported Date time data type

dfty9e19  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(154)

I have an SSIS package where I wish to store a Date Time stamp at a certain point in the package execution, that is the exact date and time at that point and store it in a SQL server table

I have a datetime column in the target table, the package connects and can write to the table because I have already done so, the date time stamp is now an added column called Process_Start and this is table creation.

I have an "execute SQL task" with SQL and configured - images show SQL task and SSIS package variables being used

A second "Execute SQL Task" processes the insert, which I know worked before this is the SQL statement in the task,

But now is failing with new date time column inserted. The error is
Error: Executing the query "INSERT INTO dbo.theTable ( " failed with the following error: "Unsupported data type on parameter binding 1.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly

The data type in the second SQL task I have tried all the Date options and increased the parameter size from 21 to 23 then 27 but it still fails. I have used breakpoints to check the StoredStartTime variable and it looks correct, i.e. 10/10/2023 14.35, although no microseconds. Why is this failing please?

brqmpdu1

brqmpdu11#

While I'm waiting on details, my solution is to use the datatype of DATE for the OLE DB Connection manager to a SQL Server RDBMS. Yes, DATE even though it has time components to it. It's a terrible name, shocking.

Variables

Two variables, both set to the year 2111 so we can validate runtime is using something else.

SQL Create target table

Create our table for the POC.

DROP TABLE IF EXISTS dbo.SO_77266997;
CREATE TABLE dbo.SO_77266997
(
    PackageName varchar(50) NOT NULL
,   ServerName varchar(50) NOT NULL
,   StartTime datetime2(7) NOT NULL
);

SQL Get datetime

Run a query to get the database's current time. Store it to our StoredStartTime variable

SQL Store datetime

A bog standard insert statement using OLE DB Parameterization (? as place holders, 0 based numbering) resulting in StoredStartTime being stored to our table

My ordinal position differs from yours but as long as things align, you'll be fine.

SQL Retrieve stored value

This query just pulls back the values we stored to prove out that we did not truncate the time component despite the data type being "DATE" Store the value to our variable RetrievedStoredStartTime

SCR Echo Back

My favorite little script as it pops the variable values to the output log which we can see the results of in the first image.

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_0371ffe3d696471a9d92b91f6527ac90
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            bool fireAgain = false;
            foreach (var item in Dts.Variables)
            {
                Dts.Events.FireInformation(0, "SCR echo back", string.Format("{0}=>{1}", item.QualifiedName, item.Value), "", 0, ref fireAgain);
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }

        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };

    }
}

相关问题