简体   繁体   English

SSIS使用脚本任务将值分配给变量

[英]SSIS Assigning value to Variable using Script Task

I am trying to assign a value to a variable from a flat file in an SSIS package. 我正在尝试为SSIS包中平面文件中的变量分配值。 The script i am using is below 我正在使用的脚本如下

#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
#End Region

' This is the class to which to add your code.  Do not change the name, attributes, or parent
' of this class.
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute()> _
<CLSCompliant(False)> _
Public Class ScriptMain
    Inherits UserComponent

    Dim StartDate As String
    Dim FinishDate As String

    Public Overrides Sub PreExecute()
        MyBase.PreExecute()

    End Sub

    Public Overrides Sub PostExecute()
        MyBase.PostExecute()
        Me.ReadWriteVariables("StartDate").Value = StartDate
        Me.ReadWriteVariables("FinishDate").Value = FinishDate

    End Sub

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        StartDate = Row.StartDate
        FinishDate = Row.FinishDate

    End Sub

End Class

The problem Im having is that sometimes the fields from the flat file could be blank, when they are blank I get an error: 我的问题是,有时平面文件中的字段可能为空,当它们为空时,我得到一个错误:

The type of the value being assigned to variable "User::StartDate" differs from the current variable type. 分配给变量“ User :: StartDate”的值的类型与当前变量类型不同。 Variables may not change type during execution. 变量在执行期间可能不会更改类型。 Variable types are strict, except for variables of type Object. 变量类型是严格的,但对象类型的变量除外。

Does anyone have any ideas what could be causing the error? 有谁知道会导致错误的原因吗?

I'm guessing that's because the value will be null when you're doing the assignment. 我猜这是因为执行分配时该值将为null。 SSIS doesn't support null values for its variables. SSIS的变量不支持空值。

What value do you want your variable to have when there are no values in the file? 当文件中没有值时,您希望变量具有什么值? And is the problem occurring when there are no rows in the file, or when there is a row but the columns are empty for StartDate and EndDate? 而当有文件中没有行,或者当行但列是空的起始日期和结束日期发生的问题?

If an empty string is what you want, try changing: 如果您想要一个空字符串,请尝试更改:

Dim StartDate As String
Dim FinishDate As String

to

Dim StartDate As String = ""
Dim FinishDate As String = ""

...to guard against the situation where there are no rows. ...以防止没有行的情况。 In that case, your ProcessInputRow will never run, so you need to set up the strings as empty from the very beginning. 在这种情况下,您的ProcessInputRow将永远不会运行,因此您需要从一开始就将字符串设置为空。

For the situation where there are rows, you want to guard against setting the values to be null in ProcessInputRow: 对于存在行的情况,您要避免在ProcessInputRow中将值设置为null:

If Not Row.StartDate_IsNull Then
    StartDate = Row.StartDate
End If

If Not Row.EndDate_IsNull Then
    EndDate = Row.EndDate
End If

Though if you didn't tick "Retain null values from the source as null values in the data flow" on the Source of the file you're using, they should be coming through as empty strings anyway... 尽管如果您没有在要使用的文件的源上打勾“在数据流中将源中的空值保留为数据流中的空值”,则无论如何它们都应该作为空字符串来传递...

If empty start and end dates are a valid scenario, you can use the 'ColumnName'_IsNull properties to guard against empty fields. 如果空的开始日期和结束日期是有效的方案,则可以使用'ColumnName'_IsNull属性来防止出现空字段。 In your code, you can add something like this: 在代码中,您可以添加以下内容:

if (String.IsNullOrEmpty(Row.StartDate))
{       
Row.StartDate_IsNull = true;
}        
else        
{            
StartDate = Row.StartDate;           
}

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

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