简体   繁体   English

SSIS-遍历列

[英]SSIS - Loop through columns

For some time now, we have been using excel files to store our purchases data. 一段时间以来,我们一直在使用excel文件存储购买数据。 This spreadsheet file has a structure that is uniform. 该电子表格文件具有统一的结构。 As it has quite a lot of attributes, I am just going to name the most important ones. 因为它具有很多属性,所以我只想列举最重要的属性。

In this Excel files we have columns for the products ID, the sizes of each product and the quantities for each composite of product id and size. 在此Excel文件中,我们具有产品ID列,每种产品的尺寸以及每种产品ID和尺寸的组合的数量。

The issue is, that we have several stores and each one of them received different quantities per product. 问题是,我们有几家商店,每个商店每种产品收到不同数量的商品。 So, in each excel, we have a column for each store, in which we place the quantities ordered from each composite. 因此,在每个excel中,我们为每个商店都有一列,其中放置了从每个组合中订购的数量。

在此处输入图片说明

So, what I want to do is to loop through the columns of each store to add the corresponding quantities of each composite to the specific store. 因此,我想做的是遍历每个商店的列,以将每种组合的相应数量添加到特定商店。

For example, 例如,

INSERT INTO MyTable (product_ID, size_ID, store_ID, quantity)
    VALUES (12345, 34, Mirkwood, 1)

and then, repeat for each column. 然后,对每列重复一次。

I have been trying to find a solution for this, but haven't figure it out just yet. 我一直在尝试为此找到解决方案,但还没有弄清楚。 I would really appreciate all the help or tips. 我真的很感谢所有帮助或提示。

Avoid trying to create some complex, dynamic, looping code in SSIS. 避免尝试在SSIS中创建一些复杂的,动态的循环代码。

It would be much simpler to split the flow so you have the product_id , size_id and data for one store. 拆分流会更简单,因此您拥有一个商店的product_idsize_id和数据。 Perform your insert for each store using its store_id , which you already know. 使用您已经知道的store_id对每个商店执行插入操作。

Although a looping solution sounds good, and might mean you don't have to update your code when you add a new store, but in SSIS it will be complex to implement and I'm guessing you are not adding stores that frequently to make the effort worthwhile. 虽然循环解决方案听起来不错,并且可能意味着添加新商店时不必更新代码,但是在SSIS中,实现起来很复杂,我猜您不是在频繁添加商店来创建新商店。努力值得。

You can use unpivot component 您可以使用unpivot组件

The columns checked will be the columns you need to transform into rows. 选中的列将是您需要转换为行的列。 Destination column is the column you will put the data in. Pivot key value column name: name of pivot columns. 目标列是将数据放入其中的列。数据透视键值列名称:数据透视表列的名称。

在此处输入图片说明

在此处输入图片说明

After that, you can just insert the rows normally. 之后,您可以正常插入行。

Another way of doing it is though sql, I am using t-sql here, but other database should have something similar. 另一种方法是使用sql,我在这里使用的是t-sql,但其他数据库也应该有类似的东西。 First import your raw data into a table. 首先将原始数据导入表中。 Then using unpivot to do something similar as above. 然后使用unpivot执行与上述类似的操作。

    CREATE TABLE #pvt (VendorID int, Emp1 int, Emp2 int,
        Emp3 int, Emp4 int, Emp5 int);
    GO
    INSERT INTO #pvt VALUES (1,4,3,5,4,4);
    INSERT INTO #pvt VALUES (2,4,1,5,5,5);
    INSERT INTO #pvt VALUES (3,4,3,5,4,4);
    INSERT INTO #pvt VALUES (4,4,2,5,5,4);
    INSERT INTO #pvt VALUES (5,5,1,5,5,5);
    GO

    select * from #pvt

    --Unpivot the table.
    SELECT VendorID, Employee, Orders – destination table, first column is row group?
    FROM 
       (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
       FROM #pvt) p – detail source
    UNPIVOT
       (Orders FOR Employee IN –orders: data, For: column group
          (Emp1, Emp2, Emp3, Emp4, Emp5)
    )AS unpvt;

The only issue with this approach is it can't handle if there is more column (store) being added. 这种方法的唯一问题是,如果添加了更多的列(存储),它将无法处理。 But you can make it dynamic: https://www.mssqltips.com/sqlservertip/3002/use-sql-servers-unpivot-operator-to-dynamically-normalize-output/ 但您可以使其动态化: https : //www.mssqltips.com/sqlservertip/3002/use-sql-servers-unpivot-operator-to-dynamically-normalize-output/

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

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