我可以在合并更新中插入(实现SCD类型2)

时间:2015-09-08 07:07:09

标签: oracle oracle11g

我有源表和我想要合并的目标表,因此应该始终在目标表中插入。对于每个更新的记录,应该更新一个标记为' Y'当这个内容发生变化时,记录标志值应该被改为“N' N'并且在该目标中插入该记录的新行,以便反映更新的记录的信息。基本上我想实现SCD type2。我的输入数据是 -

student_id   name        city                 state              mobile
1          suraj         bhopal                m.p.             9874561230
2          ravi           pune                  mh              9874563210
3          amit           patna                bihar            9632587410
4          rao           banglore               kr              9236547890
5          neel          chennai                tn              8301456987

当我输入chnages时 -

student_id     name    city     state     mobile            
   1          suraj   indore      m.p.  9874561230          

我的输出应该像 -

surr_key student_id name city state mobile insert_Date end_date   Flag
1             1   suraj bhopal m.p.9874561230 31/06/2015 1/09/2015   N
2             1   suraj indore m.p.9874561230  2/09/2015 31/12/9999  Y 

任何人都可以帮助我,我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用触发器执行此操作,您可以在目标表上插入触发器之前创建,这将更新源表的标志列。 或者您可以在源表上具有更新触发器,该触发器将在目标表中插入记录。

希望这有帮助

此致

答案 1 :(得分:0)

所以这应该是你的程序步骤的大纲。我在源和目标中使用了不同的列来简化。

  Source (tu_student) - STUDENT_ID, NAME, CITY
  Target (tu_student_tgt)- SKEY, STUDENT_ID, NAME, CITY, INSERT_DATE, END_DATE, IS_ACTIVE

这里的基本想法是

  • 从源中找到目标中缺少的新记录并插入它。将start_date设置为sysdate,将end_date设置为9999,将IsActive设置为1.
  • 查找更新的记录(例如您的博帕尔 - >印多尔案例)。所以我们必须在目标中进行2次操作

    • 更新目标中的记录,并将结束日期设置为sysdate,将IsActive设置为0.
    • 在具有新值的目标中插入此记录。将start_date设置为sysdate,将end_date设置为9999,将IsActive设置为1。

            -- Create a new oracle sequence (test_utsav_seq in this example)
      
            ---Step 1 - Find new inserts (records present in source but not in target
      
            insert into tu_student_tgt
            (
            select 
            test_utsav_seq.nextval as skey,
            s.student_id as student_id,
            s.name as name,
            s.city as city,
            sysdate as insert_date,
            '31-DEC-9999' as end_date,
            1 as Flag
            from tu_student s
            left outer join 
            tu_student_tgt t
            on s.student_id=t.student_id
            where t.student_id is null)
      
            ----Step 2 - Find skey which needs to be updated due to data chage from source and target. So get the active records from target and compare with source data. If mismatch found, we need to 
                -- a update this recods in target and mark it as Inactive.
                -- b Insert a new record for same student_id with new data and mark it Active.
      
              -- part 2a - find updates. 
               --these records need update. Save these skey and use it one by one while updating.
            select t.skey
            from tu_student s inner join
            tu_student_tgt t
            on s.student_id=t.student_id
            where t.Flag = 1 and
            (s.name!=t.name or 
            s.city!=t.city)
      
      
             --2 b ) FInd the ids which needs to be inserted as they changed in source from target. Now as above records are marked inactive,
              select s.student_id
            from tu_student s inner join
            tu_student_tgt t
            on s.student_id=t.student_id
            where t.Flag = 1 and
            (s.name!=t.name or 
            s.city!=t.city)
      
      
      
             ---2a - Implement update
            -- Now use skey from 2a in a loop and run update statements like below. Replace t.key = with the keys which needs to be updated. 
            update tu_student_tgt t
            set t.student_id = (select s.student_id from tu_student s,tu_student_tgt t  where s.student_id=t.student_id and t.key= -- id from 2a step . )
            ,  t.name=(select s.name from tu_student s,tu_student_tgt t  where s.student_id=t.student_id and t.key= --id from 2a step. )
            , end_date = sysdate
            , is_active = 0
            where t.skey = -- id from 2a step
      
            ---2b Implement Insert use student_id found in 2a
            --Insert these student id like step 1
            insert into tu_student_tgt
            (
            select 
            test_utsav_seq.nextval as skey,
            s.student_id as student_id,
            s.name as name,
            s.city as city,
            sysdate as insert_date,
            '31-DEC-9999' as end_date,
            1 as Flag
            from tu_student s
            where s.student_id = -- ID from 2b step - Repeat for other ids
      

我不能给你一个SCD-2的简单例子。如果你了解SCD-2,你应该理解这个实现。