SSIS : Using multicast to enter data into 2 RELATED destinations

前端 未结 4 515
攒了一身酷
攒了一身酷 2021-01-13 20:46

I am new to SSIS. I have data coming from a single source. I need to enter that data into several tables (these tables are related by foreign key relationships). I am usi

4条回答
  •  误落风尘
    2021-01-13 21:27

    Staging table

    CREATE TABLE [dbo].[Stage](
        [Name] [varchar](50) NULL,
        [Street] [varchar](50) NULL,
        [State] [varchar](50) NULL,
        [pkAddressID] [int] NULL
    ) ON [PRIMARY]
    
    GO
    

    Address table

    CREATE TABLE [dbo].[Address](
        [pkAddressID] [int] IDENTITY(1,1) NOT NULL,
        [street] [varchar](50) NULL,
        [state] [varchar](50) NULL
    ) ON [PRIMARY]
    

    -- Employee table

    CREATE TABLE [dbo].[Employee](
        [pkEmployeeID] [int] IDENTITY(1,1) NOT NULL,
        [fkAddressID] [int] NULL,
        [Name] [varchar](50) NULL
    ) ON [PRIMARY]
    
    GO
    

    --- DFT - Load data into Stage table

    --- Execute SQL Task 1 --- Populate Address table

    Merge [dbo].[Address] as target
    using
    (
        select distinct [Street], [State] from [dbo].[Stage]
    ) as source
    on  source.[Street] = target.[Street] and source.[State] = target.[State]
    
    when not matched then
    insert ([Street], [State])
    values (source.[Street], source.[State])
    ;
    

    --- Execute SQL Task 2 --- Populate Stage table//pkAddressID column

    Merge [dbo].[Stage] as target
    using
    (
        select [pkAddressID],[Street], [State] from [1Address]
    ) 
    as source 
    on source.[Street] = target.[Street] and source.[State] = target.[State]
    
    when matched then
    update
    set target.[pkAddressID] = source.[pkAddressID]
    ;
    

    --- Execute SQL Task 3 --- Populate Employee table

    Merge [dbo].[Employee] as target
    using
    (
        select [pkAddressID], [Name] from [dbo].[1Stage]
    ) as source
    on source.[pkAddressID] = target.[fkAddressID]
    and source.[Name] = target.[Name]
    
    when not matched then
    insert ([fkAddressID], [Name])
    values (source.[pkAddressID], source.[Name])
    ;
    

提交回复
热议问题