简体   繁体   English

SSIS将平面文件的2列(仅第一个非null)加载到变量中

[英]SSIS load 2 columns of a flat file (first non null only) into a variable

I have a flat file with the following columns 我有一个带有以下各列的平面文件

SampleID Rep_Number Product Protein Fat Solids SampleID Rep_Number产品蛋白质脂肪固体

In the flat file SampleID and Product are populated in the first row only, rest of the rows only have values for Rep_Number, Protein, Fat, Solids. 在平面文件中,SampleID和Product仅填充在第一行中,其余行仅具有Rep_Number,蛋白质,脂肪,固体的值。 SampleID and Product are blank for the rest of the rows. 其余行的SampleID和Product为空白。 So my task is to fill those blank rows with the first row that has the sampleID and Product and load into the table. 因此,我的任务是用具有sampleID和Product的第一行填充这些空白行,然后加载到表中。

So task is to pick the first non null SampleID and Product from the flat file and put them in a variable. 因此,任务是从平面文件中选择第一个非null的SampleID和Product并将其放入变量中。 And rest is all configured. 其余的都配置好了。 If I can pick the first non null SampleID and Product directly from the flat file and put into their respective variables I can take it from there. 如果我可以直接从平面文件中选择第一个非null的SampleID和Product并将其放入各自的变量中,则可以从那里获取它。 That is all I need. 这就是我所需要的。

I can connect a script component to flat file source in a data flow task. 我可以在数据流任务中将脚本组件连接到平面文件源。 I need help with the script to pick the first non null values (SampleID and Product), 我需要有关脚本的帮助,以选择第一个非null值(SampleID和Product),

Need help please. 请需要帮助。 Thanks in advance. 提前致谢。

If you are sure you need to store the data of the 1st row - 1st 2 columns' values in variables and take it from there, and DO NOT REQUIRE a change in your original approach, then try this: 如果您确定需要将第一行-第一两列的值存储在变量中,然后从那里获取,并且不需要更改原始方法,请尝试以下操作:

  1. You need to have a NEW variable to keep track of the ROW COUNT. 您需要有一个NEW变量来跟踪ROW COUNT。 Let this be an integer and set it to 0. This will help process only the first row and skip the rest. 将其设为整数并将其设置为0。这将有助于仅处理第一行而跳过其余行。 Let's call this Row_Count 我们称它为Row_Count
  2. After you retrieve data from the component connected to flat file as the source, connect it to a 'Script Component' and click 'Edit'. 从作为源文件连接到平面文件的组件中检索数据后,将其连接到“脚本组件”,然后单击“编辑”。
  3. In the 'Script Transformation Editor'click the 'Input Columns' on the left and select the desired columns (say Column_Name1 and Column_Name2) you want to retrieve the value from (ie 1st and 2nd) 在“脚本转换编辑器”中,单击左侧的“输入列”,然后选择要从中检索值(即第一和第二)的所需列(例如Column_Name1和Column_Name2)。
  4. Click 'Script' on the left 点击左侧的“脚本”
  5. Under 'Custom Properties', expand the 'ReadWriteVariables'. 在“自定义属性”下,展开“ ReadWriteVariables”。 Add the 2 variables you intend to use for storing the values AND the Row_Count variable. 添加您打算用于存储值的2个变量以及Row_Count变量。
  6. Click 'Edit Script. 点击“编辑脚本”。
  7. In the editor that opens, double click 'ScriptMain.vb' on the right. 在打开的编辑器中,双击右侧的“ ScriptMain.vb”。
  8. Under the Public Overrides Sub PostExecute() {} procedure type this: 在Public Overrides Sub PostExecute(){}过程下,键入以下内容:

    If Variables.Row_Count = 0 Then 如果Variables.Row_Count = 0则

    Variables.Your_Variable1 = Row.Column_Name1 Variables.Your_Variable1 = Row.Column_Name1

    Variables.Your_Variable2 = Row.Column_Name2 Variables.Your_Variable2 = Row.Column_Name2

    Variables.Row_Count= Variables.Row_Count + 1 Variables.Row_Count =变量.Row_Count + 1

    End If 万一

  9. You have the desired values in your variables, proceed with the rest of your logic. 您的变量中有所需的值,请继续执行其余的逻辑。

Note: 注意:

  1. If you do not add the variables to the 'ReadWriteVariables', you will not be able to access them in the script. 如果不将变量添加到“ ReadWriteVariables”中,将无法在脚本中访问它们。
  2. Based on any other code you might add in the script, you would need to include additional headers if they are not present. 根据您可能在脚本中添加的任何其他代码,如果不存在其他标头,则需要包括其他标头。

Please mark my post as answer if it helps :) 如果有帮助,请标记我的帖子为答案:)

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

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