2010-09-22 127 views
1

我在学习SSIS,这看起来像一个简单的任务,但我卡住了。SSIS使用来自.CSV文件的参数执行存储过程SQL Server 2005

我有一个CSV文件Orders.csv与此数据:

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

我也有一个存储过程ssis_createorder其作为输入参数: @ProductID INT @Quantity INT @CustomerID INT

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

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

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

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

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

我已经创建了一个名为order类型为Object的变量。我也有变量CustomerId,ProductId,& int32类型的数量。

Next我有一个Recordset Destination将.csv文件的内容分配给varialbe命令。我不确定如何使用此工具。我将VariableName(在Customer Properties下)设置为User :: orders。我认为,现在订单保存了由原始.csv文件中的内容组成的ADO记录集。

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

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

在变量映射选项卡中,我有User :: ProductId索引0,用户::数量指标1,用户::客户ID指数2.我不知道这是否是正确的。

接下来,我有一个脚本任务foreach循环容器内。

我必须设置为ReadOnlyVariables产品号

在主要方法这是我在做什么:

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 

任何帮助,将不胜感激

回答

6

我的一个同事刚给我答案。

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

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

在组件属性中输入以下的SqlCommand:

exec ssis_createorder ?, ?, ? 

的 “?”是参数的占位符。

接下来,在“列映射”选项卡下,将.csv文件列映射到存储过程参数。

你完成了,继续运行包。

谢谢加里,如果你在StackOverFlow我会给你一个upvote并接受你的答案。

+0

这似乎是处理数据的最有效方式。确保你的数据类型匹配。 – bobs 2010-09-22 19:13:37

2

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

如果您只是使用平面文件数据源创建数据流并通过3个执行sql命令任务来传输数据,该怎么办?只需将数据中的列映射到存储过程的输入参数即可。

也许我在你的问题中看不到它,我想的太简单了,但根据我的经验,你需要尽可能避免在SSIS中使用foreach任务。

+0

+1我认为foreach任务可以工作,但是您的解决方案可以节省一些工作。无论哪种方式,您必须为文件中的每一行运行一次SP。 – bobs 2010-09-22 19:03:25

+0

谢谢你的回答。 – codingguy3000 2010-09-22 19:04:42

+0

@bobs你不觉得我的回答是最好的吗? – codingguy3000 2010-09-22 19:05:32

1

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

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

1

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

+0

这将工作 – codingguy3000 2012-08-30 15:49:28

相关问题