简体   繁体   English

SSIS使用.CSV文件SQL Server 2005中的参数执行存储过程

[英]SSIS Execute a Stored Procedure with the parameters from .CSV file SQL Server 2005

I'm learning SSIS and this seems like an easy task but I'm stuck. 我正在学习SSIS,这似乎很容易,但是我被困住了。

I have a CSV file Orders.csv with this data: 我有一个包含以下数据的CSV文件Orders.csv:

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

I also have a stored procedure ssis_createorder that takes as input parameters: @productid int @quantity int @customerid int 我也有一个存储过程ssis_createorder,它使用输入参数:@productid int @quantity int @customerid int

What I want to do is create an SSIS package that takes the .csv file as input and calls ssis_createorder three times for each row in the .csv file (the first row contains column names). 我想做的是创建一个SSIS包,该包将.csv文件作为输入,并为.csv文件中的每一行调用ssis_createorder三次(第一行包含列名)。

Here is what I have done so far. 到目前为止,这是我所做的。

I have created an SSIS package (Visual Studio 2005 & SQL Server 2005). 我创建了一个SSIS包(Visual Studio 2005和SQL Server 2005)。

In Control Flow I have a Data Flow Task. 在控制流中,我有一个数据流任务。

The Data Flow has a Flat File source of my .csv file. 数据流具有我的.csv文件的平面文件源。 All of of the columns are mapped. 所有列均已映射。

I have created a variable named orders of type Object. 我已经创建了一个名为Object类型的订单的变量。 I also have variables CustomerId, ProductId, & Quantity of type int32. 我也有int32类型的变量CustomerId,ProductId和Quantity。

Next I have a Recordset Destination that is assigning the contents of the .csv file into the varialbe orders. 接下来,我有一个Recordset目标,它将.csv文件的内容分配为varialbe顺序。 I'm not sure about how to use this tool. 我不确定如何使用此工具。 I'm setting the VariableName (under Customer Properties) to User::orders. 我将VariableName(在“客户属性”下)设置为User :: orders。 I think that now orders holds an ADO record set made up of the contents from the original .csv file. 我认为现在订单包含由原始.csv文件中的内容组成的ADO记录集。

Next I'm adding a ForEach Loop Container on the Control Flow tag and linking it to the Data Flow Task. 接下来,我在Control Flow标记上添加一个ForEach循环容器,并将其链接到Data Flow Task。

Inside of the ForEach Loop Container I'm setting the Enumerator to "ForEach ADO Enumerator". 在ForEach循环容器内,我将枚举器设置为“ ForEach ADO枚举器”。 I'm setting "ADO object source variable" to User::orders". For Enumeration mode I'm selecting "Rows in the first table". 我将“ ADO对象源变量”设置为User :: orders。对于枚举模式,我选择“第一个表中的行”。

In the Variable Mapping tab I have User::ProductId index 0, User::Quantity index 1, User::CustomerId index 2. I'm not sure if this is correct. 在“变量映射”选项卡中,我具有User :: ProductId索引0,User :: Quantity索引1,User :: CustomerId索引2。我不确定这是否正确。

Next I have a Script Task inside of the ForEach Loop Container. 接下来,我在ForEach循环容器内有一个脚本任务。

I have ReadOnlyVariables set to ProductId. 我将ReadOnlyVariables设置为ProductId。

In the Main method this is what I'm doing: 在Main方法中,这就是我正在做的:

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

 MsgBox("sProductId")

When I run the package my ForEach Loop Container turns Bright Red and I get the following error messages 当我运行该程序包时,我的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

Any help would be appreciated 任何帮助,将不胜感激

One of my coworkers just give me the answer. 我的一位同事给我答案。

You don't need the the ForEach Loop Container or the RecordSet Container. 您不需要ForEach循环容器或RecordSet容器。

All you need is the Flat File Source and an OLE DB Command. 您需要的只是平面文件源和OLE DB命令。 Connect to your database and inside the OLE DB Command select the appropriate connection. 连接到数据库,然后在OLE DB命令中选择适当的连接。

In the Component Properties enter the following SQLCommand: 在“组件属性”中,输入以下SQLCommand:

exec ssis_createorder ?, ?, ? 

The "?" “?” are place holders for the parameters. 是参数的占位符。

Next under the Column Mappings tab map the .csv file columns to the stored procedure parameters. 接下来,在“列映射”选项卡下,将.csv文件列映射到存储过程参数。

You are finished go ahead and run the package. 您完成操作并运行程序包。

Thanks Gary if you were on StackOverFlow I would give you an upvote and accept your answer. 谢谢加里,如果您在StackOverFlow上,我会给您赞成,并接受您的回答。

If I understand correctly, what you want to do is execute a stored procedure 3 times for each row in the data source. 如果我理解正确,那么您要做的就是对数据源中的每一行执行一次存储过程3次。

What if you just create a data flow with a flat file data source and pipe the data through 3 execute sql command tasks? 如果仅使用平面文件数据源创建数据流并通过3个执行sql命令任务通过管道传输数据怎么办? Just map the columns in the data to the input params of your stored procedure. 只需将数据中的列映射到存储过程的输入参数即可。

Maybe I'm not seeing it correctly in your question and I'm thinking too simple, but in my experience you need to avoid using the foreach task in SSIS as much as possible. 也许我在您的问题中看不正确,并且我想的太简单了,但是以我的经验,您需要尽可能避免在SSIS中使用foreach任务。

I'm not sure if this answers your question. 我不确定这是否能回答您的问题。 But I was looking to do this and I achieved it using the BULK INSERT command. 但是我一直想做到这一点,并使用BULK INSERT命令实现了它。 I created a staging table with all of the columns in the csv file, and instead of a stored procedure I used a INSTEAD OF INSERT trigger to handle the logic of inserting it into many tables. 我创建了一个暂存表,其中包含csv文件中的所有列,并且我使用了INSTEAD OF INSERT触发器来代替将存储过程插入到许多表中的逻辑,而不是使用存储过程。

I suspect that you need to look at your Data Flow task. 我怀疑您需要查看数据流任务。 It's likely that the values from the source CSV file are being interpreted as string values. 源CSV文件中的值很可能被解释为字符串值。 You will probably need a Derived Column component or a Data Conversion component to convert your input values to the desired data type. 您可能需要“派生列”组件或“数据转换”组件,才能将输入值转换为所需的数据类型。

And, I think @StephaneT's solution would be good for executing the SP. 而且,我认为@StephaneT的解决方案对于执行SP非常有用。

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

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