【问题标题】:SSIS Execute a Stored Procedure with the parameters from .CSV file SQL Server 2005SSIS 使用 .CSV 文件 SQL Server 2005 中的参数执行存储过程
【发布时间】:2010-09-22 18:34:20
【问题描述】:

我正在学习 SSIS,这似乎是一项简单的任务,但我被困住了。

我有一个包含这些数据的 CSV 文件 Orders.csv:

ProductId,Quantity,CustomerId
1,1,104
2,1,105
3,2,106

我还有一个存储过程 ssis_createorder 作为输入参数: @productid 整数 @数量整数 @customerid 整数

我想要做的是创建一个 SSIS 包,它将 .csv 文件作为输入,并为 .csv 文件中的每一行调用 ssis_createorder 三次(第一行包含列名)。

这是我到目前为止所做的。

我创建了一个 SSIS 包(Visual Studio 2005 和 SQL Server 2005)。

在控制流中,我有一个数据流任务。

数据流具有我的 .csv 文件的平面文件源。所有列都已映射。

我创建了一个名为 Orders 类型的 Object 变量。我还有 int32 类型的变量 CustomerId、ProductId 和 Quantity。

接下来我有一个记录集目标,它将 .csv 文件的内容分配给变量订单。我不确定如何使用这个工具。我将变量名称(在客户属性下)设置为 User::orders。我认为现在的订单包含一个由原始 .csv 文件的内容组成的 ADO 记录集。

接下来,我将在控制流标记上添加一个 ForEach 循环容器并将其链接到数据流任务。

在 ForEach 循环容器内部,我将枚举器设置为“ForEach ADO 枚举器”。我将“ADO 对象源变量”设置为 User::orders。对于枚举模式,我选择“第一个表中的行”。

在变量映射选项卡中,我有 User::ProductId index 0、User::Quantity index 1、User::CustomerId index 2。我不确定这是否正确。

接下来我在 ForEach 循环容器中有一个脚本任务。

我已将 ReadOnlyVariables 设置为 ProductId。

在 Main 方法中,这就是我正在做的事情:

 Dim sProductId As String = Dts.Variables("ProductId").Value.ToString

 MsgBox("sProductId")

当我运行包时,我的 ForEach 循环容器变为亮红色,并且我收到以下错误消息

Error: 0xC001F009 at MasterTest: The type of the value being assigned to variable "User::ProductId" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 1 to variable "User::ProductId" cannot be applied.
Error: 0xC001F009 at MasterTest: The type of the value being assigned to variable "User::Quantity" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 2 to variable "User::Quantity" cannot be applied.
Error: 0xC001F009 at MasterTest: The type of the value being assigned to variable "User::CustomerId" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 3 to variable "User::CustomerId" cannot be applied.
Warning: 0x80019002 at MasterTest: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (12) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "Package.dtsx" finished: Failure.
     Dts.TaskResult = Dts.Results.Success

任何帮助将不胜感激

【问题讨论】:

    标签: sql-server sql-server-2005 ssis


    【解决方案1】:

    我的一位同事刚刚给了我答案。

    您不需要 ForEach 循环容器或 RecordSet 容器。

    您只需要平面文件源和 OLE DB 命令。连接到您的数据库并在 OLE DB 命令中选择适当的连接。

    在组件属性中输入以下 SQLCommand:

    exec ssis_createorder ?, ?, ? 
    

    “?”是参数的占位符。

    下一步在列映射选项卡下,将 .csv 文件列映射到存储过程参数。

    你已经完成了继续运行包。

    感谢 Gary,如果您在 StackOverFlow 上,我会给您一个赞成票并接受您的回答。

    【讨论】:

    • 这似乎是处理数据的最有效方式。确保您的数据类型匹配。
    【解决方案2】:

    如果我理解正确,您要做的是为数据源中的每一行执行 3 次存储过程。

    如果你只是创建一个带有平面文件数据源的数据流,并通过 3 个执行 sql 命令任务来管道数据呢?只需将数据中的列映射到存储过程的输入参数。

    也许我在您的问题中没有正确看到它,而且我的想法太简单了,但根据我的经验,您需要尽可能避免在 SSIS 中使用 foreach 任务。

    【讨论】:

    • +1 我认为 foreach 任务会起作用,但您的解决方案可以节省一些工作。无论哪种方式,您都必须为文件中的每一行运行一次 SP。
    • @bobs 你不觉得我的回答是最好的吗?
    • 这两种解决方案听起来很相似。不过,您似乎在数据流中只需要一个 OLE DB 命令组件。
    • 当然你可以在一个任务中调用 3 个存储过程调用,但是你在做三件不同的事情并不是很明显 :)
    【解决方案3】:

    我怀疑您需要查看您的数据流任务。源 CSV 文件中的值很可能被解释为字符串值。您可能需要派生列组件或数据转换组件来将输入值转换为所需的数据类型。

    而且,我认为@StephaneT 的解决方案对于执行 SP 很有用。

    【讨论】:

      【解决方案4】:

      我不确定这是否能回答您的问题。但我想这样做,我使用 BULK INSERT 命令实现了它。我创建了一个包含 csv 文件中所有列的临时表,而不是存储过程,我使用 INSTEAD OF INSERT 触发器来处理将其插入多个表的逻辑。

      【讨论】:

        猜你喜欢
        • 2016-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 2010-10-29
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多