![](/img/trans.png)
[英]How to avoid re-inserting data (duplicates) into SQL Server table while re-running SSIS package that loads data?
[英]how to avoid re-inserting data into sql table while re-running SSIS package that loads data from flat file to SQL table?
我有一個包含以下數據的平面文件
Id,FirstName,LastName,Address,PhoneNumber
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
4,nash,gordon,charlotte NC ADDRESS,111200110
我創建了一個具有平面文件源的SSIS包,以及一個聚合函數,該函數確保僅插入唯一的行,並且不重復來自平面文件的記錄,並且將SQL表作為目標。
當我運行軟件包時,一切都很好,我得到的是SQL表中的輸出以下
Id FName LName Address phoneNumber
1 ben afflick xyz Address 5014123746
4 nash gordon charlotte NC ADDRESS 111200110
3 christina smith test address 111000110
但是當我如下向平面文件添加一些新數據時
Id,FirstName,LastName,Address,PhoneNumber
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
4,nash,gordon,charlotte NC ADDRESS,111200110
5,abc,xyz,New York,9999988888
並重新運行該包,如下表所示重新插入表中已經存在的數據
1 ben afflick xyz Address 5014123746
4 nash gordon charlotte NC ADDRESS 111200110
3 christina smith test address 111000110
1 ben afflick xyz Address 5014123746
5 abc xyz New York 9999988888
4 nash gordon charlotte NC ADDRESS 111200110
3 christina smith test address 111000110
但是我不想要這個,我不希望插入已經存在的數據。 我只希望將新添加的數據插入到SQL表中。
有人可以幫我實現這一目標嗎?
您的數據流任務會是這個樣子。 在這里,平面文件源讀取CSV文件,然后將數據傳遞給Lookup轉換。 此轉換將檢查目標表中是否存在現有數據。 如果沒有匹配的記錄,則CSV文件中的數據將被發送到OLE DB目標,否則,該數據將僅被丟棄。 查找轉換鏈接---
http://www.codeproject.com/Tips/574437/Term-Lookup-Transformation-in-SSIS
另一種方法是將文件加載到一個臨時表在數據庫中,然后用MERGE語句將數據插入到目標表。
在實踐中,這看起來像從平面文件到您的臨時表的數據流,然后執行包含MERGE語句SQL任務。 然后,您也可以根據需要更新任何匹配的值。
merge into table_a
using stage_a
on stage_a.key = table_a.key
when not matched then insert (a,b,c,d) values ( a,b,c,d )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.