dbTalk Databases Forums  

Re: Can this be done with a DTS package?

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Re: Can this be done with a DTS package? in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jacco Schalkwijk
 
Posts: n/a

Default Re: Can this be done with a DTS package? - 07-01-2004 , 02:53 PM






I would pump the contents of the text file into a staging table. Then update
the existing table via an inner join and insert the new rows from the
staging table into the existing table using an outer join.

I.e.:
- Create staging table (SQL task)
- Pump data from text file into staging table
- UPDATE m SET <column 1> = s.<column 1>, <column 2> = s.<column 2> etc
FROM main_table m INNER JOIN staging_table s
ON m.<primary key> = s.<primary key>
- INSERT INTO main_table (<column list>)
SELECT <column list>
FROM staging_table s
LEFT OUTER JOIN main_table m
ON m.<primary key> = s.<primary key>
WHERE m.<primary key> IS NULL
- Drop staging table

--
Jacco Schalkwijk
SQL Server MVP

"A Fu" <AFu (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hello,

I need to use a csv text file to update/add records in db tables. I have
to loop through each row in the text file, use an ID column to search a db
table for any existing records, if a record is found, then do an update, if
not, then do an insert.
Quote:
How exactly can I do this with DTS package? Which task(s) should I use?

Any advice is greatly appreciated.
Thanks

A Fu



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.