Author |
Topic |
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2008-08-28 : 09:23:20
|
Morning Chiefs,I have a situatiuon at hand.. Please I need your assistanceI have 2 table .. Table A and TableBoth have same columns and same in number too...Table A is staging table(BI people, :P), this contains new records, Table B contains History..I want delete the content of Table A from Table B and re-insert all over again if exists, else It insert fresh record...This is the logic am thinking of, a better logic will be highly appreciated.Thanks you all, folks.'Nice day aheadI sign for fame not for shame but all the same, I sign my name. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-28 : 09:45:48
|
delete and re-insert? didnt understand why you want to do that. can you illustrate with some data? |
|
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2008-08-28 : 09:56:04
|
quote: Originally posted by visakh16 delete and re-insert? didnt understand why you want to do that. can you illustrate with some data?
Am afraid I might have a wrong logic.. what about check if exists,I sign for fame not for shame but all the same, I sign my name. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-28 : 09:58:46
|
quote: Originally posted by abacusdotcom
quote: Originally posted by visakh16 delete and re-insert? didnt understand why you want to do that. can you illustrate with some data?
Am afraid I might have a wrong logic.. what about check if exists,I sign for fame not for shame but all the same, I sign my name.
Can you give some data and explain what you're trying to do. Without knowing your background its difficult to suggest what you need. |
|
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2008-08-28 : 10:12:17
|
quote: Originally posted by visakh16 delete and re-insert? didnt understand why you want to do that. can you illustrate with some data?
Good day visakh16Am afraid I might have a wrong logic.. ok this is what I want..STEPS 1. check if exists the whole records in TABLE A in TABLE B 2. if exists replace the records in TABLE B with TABLE A 3. if not exists insert allThanksAllow me give you the scenario am working... I have a table called Budget, it has the following rows |ID|Field|Period|Amount| .. I source data from excel document, using ETL of SSIS... I then dump into a temp table. I want to dump the temp into budget table, but i dont want to duplicate any records, so checking by year and month, i will know if it exist or not.. if it exist remove and insert (or update) else insert...thanks I sign for fame not for shame but all the same, I sign my name. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-28 : 10:42:38
|
ok. i think you just need below--records in temp not in budget so new ones and insertINSERT INTO Budget (fields...)SELECT t.*FROM Temp tLEFT JOIN Budget bON b.Year=t.yearAND b.month=t.monthWHERE b.year IS NULLAND b.month IS NULL --records in both temp and Budget so updateUPDATE bSET b.field1=t.field1,...FROM Temp tINNER JOIN Budget bON b.Year=t.yearAND b.month=t.month --records in Budget not in Temp...so deleteDELETE bFROM Budget bLEFT JOIN Temp tON b.Year=t.yearAND b.month=t.monthWHERE t.year IS NULLAND t.month IS NULL and remember to replace * by actual field names and also in select list |
|
|
abacusdotcom
Posting Yak Master
133 Posts |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-08-28 : 14:13:52
|
Or use SSIS package if you want performance gain:1)Oledb task2)Lookup task(excludes if find matches and updates or insert)3)Oledb destination. |
|
|
|