ETL SQL Server 2008 - 如何处理不匹配的记录?

时间:2012-11-08 15:35:10

标签: sql sql-server sql-server-2005 etl

我是ETL的新手。我正在研究这个过程,它的作用是加载人的生日。这是从事实表加载到不同的表。没有任何关键可以加入它们,涉及的所有三个表。我将拆分名称字段并与拆分名称匹配。这会加载匹配的,但我的问题是我必须将不匹配的加载到不同的第三个表。我该怎么做那个逻辑?

update FactTableEmp F
       set Bdate = S.Birthdate

from   FactTableEmp
       cross apply dbo.split(Name) as u
       join SourceTableEmp S on u.Fname = s.FirstName and u.LName = S.Lastname  

--Is using the one below going to work using the same way above?
u.Fnames != S.FirstName
u.Lname != S.Lastname

2 个答案:

答案 0 :(得分:1)

使用MERGE语句(参见示例 D。将MERGE语句的结果插入另一个表),您应该能够将ETL过程简单地转换为单个语句。

答案 1 :(得分:0)

INSERT dbo.third_table
SELECT Bdate, Name
FROM (
      MERGE dbo.FactTableEmp AS target
      USING (
             SELECT x.Bdate,
                    x.Name,
                    u.FName,
                    u.LName, 
                    s.Birthdate,
                    s.FirstName,
                    s.LastName
             FROM dbo.FactTableEmp x CROSS APPLY dbo.split(x.Name) u
               FULL JOIN dbo.SourceTableEmp s ON u.FName = s.FirstName and u.LName = S.Lastname
      ) AS source
ON (target.Name = source.Name AND source.FName = source.FirstName AND source.LName = source.Lastname)
WHEN MATCHED 
  THEN UPDATE SET target.Bdate = source.Birthdate
WHEN NOT MATCHED BY SOURCE
  THEN UPDATE SET target.Name = target.Name
OUTPUT INSERTED.Bdate, INSERTED.Name) AS Changes(Bdate, Name)
WHERE Bdate IS NULL;