2015-07-19 2615 views
1

我需要采取非标准化数据并创建标准化表。SSIS 2012连接表

我不知道如何获取详细信息ID插入到连接表(StoreAddress.AddressID在下面的示例中)。

如果我是在循环内作为T-SQL中的存储过程执行此操作,那么在将地址行插入到Address中以获取密钥并将其插入StoreAddress后,将使用@@ IDENTITY。我无法弄清楚如何在SSIS 2012中使用变换。

以存储和地址为例。

输入数据看起来像:

Store, Type, Address1, City, State 
1, P, 123 Main, Central, PA 
1, M, 123 Second, Central, PA 
2, P, 123 Third, Pokono, NY 
2, M, 123 Third, Pokono, NY 

目的地表格是: 存储(已经填充在不同的数据流)

StoreID, StoreName, StoreNumber 
9878, Main street, 1 
561, Mountain View, 2 

地址类型(已填充在不同的数据流)

AddressTypeID, Code, Description 
1, P, Physical 
2, M, Mailing 
3, O, Other 

地址

AddressID, Addr1, City, State 
721, 123 Main, Central, PA 
843, 123 Second, Central, PA 
1098, 123 Third, Pokono, NY 

StoreAddress

StoreID, AddressID, AddressTypeID 
9878, 721, 1 
9878, 843, 2 
561, 1098, 1 
561, 1098, 2 

我想这应该是一个相当普遍的转型并没有完成它在SSIS最佳实践。

感谢您考虑我的问题!

回答

2

开始通过插入不同的地址:

INSERT dbo.Address (Addr1, City, State) 
SELECT DISTINCT Address1, City, State 
FROM input; 

(也许有:如果你在那里有价值观已经NOT EXISTS)

然后使用查找,为获取值的StoreAddress表。

INSERT dbo.StoreAddress (StoreId, AddressId, AddressTypeID) 
SELECT 
    (SELECT s.StoreId from dbo.Store AS s 
    WHERE s.StoreNumber = i.Store) 
, (SELECT a.AddressId FROM dbo.Address AS a 
    WHERE a.Addr1 = i.Address1 
    AND a.City = i.City 
    AND a.State = i.State) 
, (SELECT at.AddressTypeId 
    FROM dbo.AddressType AS at 
    WHERE at.Code = i.Type) 
FROM input AS i; 

使用子查询这样就像使用加入,但更安全,因为你不会在input影响的行数。

在纯SSIS中,执行一个数据流任务来使地址表排序,只使用地址列上的排序,并将其打开。然后,您可以使用三次查找转换来完成另一个数据流任务,以获取上面我写的查询中的ID。

+0

感谢您的详细解答!这当然是比基于代码的循环更基于Set的SQL方法。但是它似乎没有利用SSIS转换框架。我打算将问题留待一段时间,看看是否有其他人回复了SSIS的特定方法。 –

+0

为SSIS编辑 –

+0

不错!我会尝试两种,看看哪个更好! –