sql-server - 如何使用 SSIS 通过 foreach 循环容器调整记录集中的变量类型

标签 sql-server excel ssis etl

我被 SSIS 困住了。当我尝试使用 foreach 容器循环遍历一个记录集时,我从 Excel 文件中提取数据。

Enter image description here

这是我使用的两个变量:

Enter image description here

我做的第一件事是从 Excel 文件中提取数据,然后将其作为记录集存储到变量“CriteriaResult”中。

Enter image description here

Enter image description here

然后在 foreach 循环中,我使用 ADO 枚举器来访问此记录集:

Enter image description here

此记录集中的每条记录都将转换为字符串变量“CriteriaID”:

Enter image description here

然后我会将此记录插入到 SQL Server 中的表中:

Enter image description here

参数设置与SQL Server表中列的数据类型相同:

Enter image description here

但是,我在 foreach 循环步骤中遇到错误:

Enter image description here

Error: 0xC001F009 at Get excel data: The type of the value being assigned to variable "User::CriteriaID" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.

Excel文件中的原始数据是这样的:

Enter image description here

它从数字 1 和 2 开始,然后更改为 3A、3B...我想这就是它在 foreach 循环步骤中声明数据类型更改的原因。在循环之前,我可以添加任何脚本任务来格式化记录集中的数据吗?

另一个潜在问题可能来自合并单元格。不过我之前尝试过单格,效果很好。

如何解决这个问题?

最佳答案

尝试使用表达式而不是传递参数:

  • 不要指定任何参数
  • 转到表达式选项卡 → SqlStatement 属性
  • 使用以下表达式:

    "INSERT INTO dbo.SSIS (CriteriaID) VALUES(" + @[User::CriteriaID] + ")"
    

关于sql-server - 如何使用 SSIS 通过 foreach 循环容器调整记录集中的变量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48798148/

相关文章:

sql-server - 使用索引排序

c# - 跨 SSIS 解决方案中的所有包克隆或删除特定变量、组件和任务

ssis - 将查找转换部分缓存限制为加载的值

excel - 当重要的输入文本框为空时如何生成错误

sql-server - SSIS 2008 : Can a package configuration contain a property not used by the package?

sql - 指定哪一列在前 SQL

c# - SQLException (0x80131904) : Invalid object name 'dbo.Categories'

sql - 仅针对当前财政年度日期的动态 SQL

excel - 当自动触发公式时,如何更改我的 Worksheet_Change 代码以更改单元格的值

vba - 将文本转换为数字