2015-09-08 32 views
0

我有源表和目標表我想做的合併,這樣應該總是在目標表中插入。對於更新過的每條記錄,應該有一個標誌更新爲'Y',並且當這些東西改變時,記錄標誌值應該chnaged爲'N',並且該記錄的新行插入到目標中,使得記錄的信息應該反映更新。基本上我想實施SCD type2。 IS-我可以插入更新合併(執行SCD類型2)

student_id name  city     state    mobile 
1   suraj   bhopal    m.p.    9874561230 
2   ravi   pune     mh
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 

誰能幫助我,我該怎麼辦呢?

+0

請發佈創建和插入語句並顯示您所需的輸出。 –

+0

所以最初的城市是indore。現在它來源於博帕爾?你想爲博帕爾旗將N? – Utsav

+0

初始城市是博帕爾,現在的城市是吲哚並且是博帕爾的N,因爲它不活躍 –

回答

0

您可以使用觸發器執行此操作,您可以在目標表上插入觸發器之前創建,這將更新源表的標誌列。 或者你可以在更新後觸發源表,它將在目標表中插入記錄。

希望這有助於

問候,

+0

謝謝你,但我不想用觸發任何其他想法? –

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
    • 插入此記錄在目標具有新的價值。設置起始日期爲SYSDATE日期,結束日期爲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,你應該理解這個實現。

+0

在這個例子中,你對兩個記錄都使用相同的product_id。產品ID是否來自源?如果沒有,那麼您的示例中的product_id應該不同。理想情況下,它應該在目標中自動生成,如果它不是從源代碼生成的。 – Utsav

+0

是的,我很抱歉,應該改變product_id,所以當我遇到這種情況時,我將如何檢查哪個產品ID已經在tgt中,因爲我不能在子查詢中使用插入,因爲它會導致錯誤,並且在步驟I中手動插入? –

+0

所以在這種情況下,在步驟1中,產品名稱應該是標識符。產品ID應該在目標中自動生成。否則,在步驟3中,使用目標中的max(product_id),同時插入並附加1.我的回答基於您的示例。 – Utsav

相關問題