Groups | Blog | Home
all groups > sql server dts > april 2007 >

sql server dts : How to delete a row before insert if the key exist?


Amiram Korach
4/29/2007 4:26:02 AM
I have to identical tables in two different servers. I want to insert the
rows from server A to server B, where these rows doesn't exist in B.
For those who exist in B, I want to delete them from B before the insert and
then insert them too.
I managed to insert the rows but not to delete the rows that exist, which
raise an error if there are similar keys.
I saw some very compilcated solutions to the problem, and I'm sure there is
a simple solution. Anyone knows?
Allan Mitchell
4/30/2007 8:22:07 PM
Hello Amiram,

This is certainly possible but you will need to be careful around constraints
and data integrity.

I personally would use the MERGE JOIN transform to do the data correlation
(LEFT OUTER JOIN)
I would then use a conditional split to decide what I wanted to do with the
rows of data that came from here.

Say I return the key column from the right hand side of the LEFT OUTER JOIN
if there is no match then it will come back as NULL so in the conditional
split I can test for this

ISNULL([KeyCol])

I would then throw these rows in to a staging table and after the data flow
task I would use a set based DELETE followed by an INSERT.

Make sense?


--

Allan Mitchell
http://wiki.sqlis.com | http://www.sqlis.com | http://www.sqldts.com |
http://www.konesans.com

[quoted text, click to view]

Amiram Korach
5/1/2007 1:59:02 AM
Thanks for your reply.
This solution is alittle more complicated than I need. I finally managed to
use the OLE command to delete the rows before the insert.
Thanks.

[quoted text, click to view]
Allan Mitchell
5/1/2007 4:32:00 PM
Hello Amiram,


Just remember the OLE DB Command transform is or at least can be resource
intensive as it executes for every row of data coming through it. Over small
data sets this is fine but larger ones can cause issues. Glad you sorted
things though.

--

Allan Mitchell
http://wiki.sqlis.com | http://www.sqlis.com | http://www.sqldts.com |
http://www.konesans.com

[quoted text, click to view]

AddThis Social Bookmark Button