SSIS populating cross reference table

独自空忆成欢 提交于 2019-12-04 06:11:45

问题


Destination tables look like this:

Source table look like this:

Customer

CustomerId FirstName LastName Email Address1 Address2 City Zip

Person table in destination is a base table (which will later be inherited by new customer table). So I am trying to export a row from one table and populate 3 tables in destination.

I managed to do this in following way:

  1. Get records from source table (Customer)
  2. Create empty AddressId field
  3. Populate Address table using OLE DB Command task (it calls stored procedure which returns SCOPE_IDENTITY() that's mapped to AddressId field)
  4. Repeat step 3 for populating Person table (and retrieving PersonId
  5. Populate cross reference table PersonAddress using PersonId and AddressId fields

Screenshot of this package is below.

Biggest issue with this approach is that OLE DB Command task is inserting row by row and it makes the whole package extremely slow. Is it possible to achieve the same thing but using fast load?

I am able to do it using OLE DB Command task which calls the stored procedure and then


回答1:


I don't think you need SSIS. You can use OUTPUT clause of INSERT which returns all identity keys to a temporary table

Lets try reproduce your scenario...


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


来源:https://stackoverflow.com/questions/28605695/ssis-populating-cross-reference-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!