2010-09-22 45 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

相關問題