SSIS填充交叉引用表

时间:2015-02-19 11:52:45

标签: sql ssis identity cross-reference

目标表如下所示:

enter image description here

源表如下所示:

客户

  

CustomerId FirstName LastName电子邮件地址1地址2城市邮编

目标中的人员表是一个基表(稍后将由新的客户表继承)。所以我试图从一个表中导出一行并在目标中填充3个表。

我设法按照以下方式执行此操作:

  1. 从源表(客户)获取记录
  2. 创建空的AddressId字段
  3. 使用OLE DB Command任务填充地址表(它调用存储过程,该存储过程返回映射到AddressId字段的SCOPE_IDENTITY())
  4. 重复步骤3以填充Person表(并检索PersonId
  5. 使用PersonId和AddressId字段填充交叉引用表PersonAddress
  6. 此软件包的屏幕截图如下。

    这种方法的最大问题是OLE DB Command任务是逐行插入的,它使整个包非常慢。是否有可能实现相同的目标,但使用快速负载?

    enter image description here

    我能够使用OLE DB Command任务调用存储过程然后

1 个答案:

答案 0 :(得分:0)

我认为你不需要SSIS。 您可以使用INSERT的OUTPUT子句将所有身份密钥返回到临时表

让我们尝试重现你的场景......


set nocount on

go
create table Customer (CustomerId int, CustomerName varchar(100) null, Address1 varchar(100) null, Address2 varchar(100) )
create table [Person] (PersonId int identity, PersonName varchar(100) null)
create table [Address] (AddressId int identity, AddressLine varchar(100) null)
create table [PersonAddress] (AddressId int, PersonId int )
go

-- create some data...
insert into Customer (CustomerId)   values ( 1000000 +  convert(int,  RAND() * 1000000) )
go 1000 

update Customer
set CustomerName = 'CustomerName ' + convert(varchar, CustomerId),
    Address1 = 'Address1 ' + convert(varchar, CustomerId),
    Address2 = 'Address2 ' + convert(varchar, CustomerId)
go



declare @identities_Person table ([rownumber] int identity,   id int)
declare @identities_Address table ([rownumber] int identity,  id int)

insert into Person (PersonName)
output  inserted.PersonId into @identities_Person
select
    c.CustomerName      
from  Customer c 
order by c.CustomerId

insert into [Address] (AddressLine)
output  inserted.AddressId into @identities_Address
select
    c.Address1
from  Customer c 
order by c.CustomerId

insert into [PersonAddress] (PersonId, AddressId)           
select p.id, a.id
from @identities_Address a
inner join @identities_Person p on p.rownumber = a.rownumber

select *
from PersonAddress pa
inner join [Address] a on a.AddressId = pa.AddressId
inner join [Person] p on p.PersonId = pa.PersonId