简体   繁体   中英

ETL SQL Server 2008 - How to handle non-matching records?

I am fairly new to ETL. I am working on this proc and what it does is it loads birthdates of people. This is loaded from a fact table onto a different table. There is no key to join them by, all the three tables involved. I am going to split the Name field and do match with the split name. This loads the matching ones, but my question is I have to load the non matching ones to a different third table. How do I do that logic?

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

使用MERGE语句(请参见示例D.将MERGE语句的结果插入到另一个表中 ),您应该能够将ETL流程简化为单个语句。

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM