暂存表
CREATE TABLE [dbo].[Stage](
[Name] [varchar](50) NULL,
[Street] [varchar](50) NULL,
[State] [varchar](50) NULL,
[pkAddressID] [int] NULL
) ON [PRIMARY]
GO
地址表
CREATE TABLE [dbo].[Address](
[pkAddressID] [int] IDENTITY(1,1) NOT NULL,
[street] [varchar](50) NULL,
[state] [varchar](50) NULL
) ON [PRIMARY]
-- 员工表
CREATE TABLE [dbo].[Employee](
[pkEmployeeID] [int] IDENTITY(1,1) NOT NULL,
[fkAddressID] [int] NULL,
[Name] [varchar](50) NULL
) ON [PRIMARY]
GO
--- DFT - 将数据加载到 Stage 表中
--- 执行 SQL 任务 1 --- 填充地址表
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])
;
--- 执行 SQL 任务 2 --- 填充阶段表//pkAddressID 列
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]
;
--- 执行 SQL 任务 3 --- 填充 Employee 表
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])
;