简体   繁体   English

SSIS 脚本组件将 Object 类型变量转换为 Int 变量

[英]SSIS Script Component convert Object type variable to Int Variable

I need to load data from ODBC source to OLEDB destination.我需要将数据从 ODBC 源加载到 OLEDB 目标。 The OLEDB destination table does not contain Identity column but it does have a Primary key column where the values has to be inserted similar to Identity column(increment it by 1). OLEDB 目标表不包含标识列,但它确实有一个主键列,其中必须插入类似于标识列的值(将其增加 1)。

I have queried the destination table(OLEDB Source) and have retrieved the max(Id) and assigned to an SSIS variable of Object type and using Script Component from Data Flow to get the max(Id)+1 value row by row.我查询了目标表(OLEDB 源)并检索了 max(Id) 并分配给 Object 类型的 SSIS 变量,并使用数据流中的脚本组件逐行获取max(Id)+1值。

While converting the Object type variable to Integer type, I get error as将 Object 类型变量转换为 Integer 类型时,出现错误

Unable to cast COM object of type 'System.__ComObject' to interface type 'System.IConvertible'.无法将“System.__ComObject”类型的 COM object 转换为接口类型“System.IConvertible”。 This operation failed because the QueryInterface call on the COM component for the interface with IID '{805E3B62-B5E9-393D-8941-377D8BF4556B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).此操作失败,因为对具有 IID '{805E3B62-B5E9-393D-8941-377D8BF4556B}' 的接口的 COM 组件的 QueryInterface 调用失败,原因是以下错误:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .

The column from destination table can not be altered.无法更改目标表中的列。

MaxUID = Object Variable MaxUID = Object 变量

RowNumber = Output variable行号 = Output 变量

Script Component Code脚本组件代码

int i = 1;

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    Row.RowNumber = Convert.ToInt32(this.Variables.MaxUID) + 1;
    i = i + 1;
}

This are the steps you need:这是您需要的步骤:

  1. Prepare an OleDb Connection to the database you intend to query the MAX(id) from.准备一个与您打算从中查询MAX(id)的数据库的 OleDb 连接。

  2. Declare a Variable MaxUID of type Int32 (or Int64 if necessary)声明一个 Int32 类型的变量MaxUID (或 Int64,如果需要)

  3. Add an "Execute SQL Task" to your package.将“执行 SQL 任务”添加到 package。

  4. Configure your "Execute SQL Task" like this:像这样配置您的“执行 SQL 任务”:

    "General" Tab “常规”选项卡
    ResultSet = "Single Row" ResultSet = "单行"
    SourceType = "Direct Input" SourceType = "直接输入"
    SQL Statement = SELECT max(Id)+1 FROM your_table_name AS [MaxUID] SQL 语句= SELECT max(Id)+1 FROM your_table_name AS [MaxUID]

    "Result Set" Tab “结果集”选项卡
    Result Name = MaxUID结果名称= MaxUID
    Variable Name = User::MaxUID变量名称= User::MaxUID

Now you have a Variable of type integer you can use in your Script Task.现在您有一个类型为 integer 的变量,您可以在脚本任务中使用。


Side note: If you only use this script task to increment the MaxUID value would an Expression Task be much easier?旁注:如果您只使用此脚本任务来增加 MaxUID 值,那么表达式任务会容易得多吗?

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

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