dbTalk Databases Forums  

Insert Update query problem in DTS

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


Discuss Insert Update query problem in DTS in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joey D
 
Posts: n/a

Default Insert Update query problem in DTS - 08-10-2004 , 04:24 PM






Hi,

I have set up DTS to import a csv file into a scratch table. From here I
have another connection to the final destination table.

But before going to the Final Table I have added a DDQ to check for insert
or update depending on wether the record exists.

Text File ------> Scratch Table ------> Final Table
Quote:


DDQ

I have the DDQ updating if record in final table exists and inserting if
not. At least it is suppose to.

Here is my query I have in the "Queries" tab for the "Update" of the DDQ
properties...


---start code---
UPDATE tblContacts
SET CONT_ID = ?, FIRSTNAME = ?, LASTNAME = ?, EMAIL_ADR = ?
WHERE CONT_ID = ? IF EXISTS

(SELECT *
FROM tblContactsScratch
WHERE CONT_ID = ?)

UPDATE tblContacts
SET CONT_ID = ?, FIRSTNAME = ?, LASTNAME = ?, EMAIL_ADR = ?

WHERE (CONT_ID = ?)

ELSE

INSERT INTO
tblContacts(CONT_ID, FIRSTNAME, LASTNAME, EMAIL_ADR)
VALUES (?, ?, ?, ?)
---end code---

Here is what my transformation looks like.


---startcode---
'************************************************* *********************
' Visual Basic Transformation Script
'************************************************* ***********************

' Copy each source column to the destination column
Function Main()
' the following will be used as parameters in place of ?
DTSDestination("CONT_ID") = DTSSource("CONT_ID")
DTSDestination("FIRSTNAME") = DTSSource("FIRSTNAME")
DTSDestination("LASTNAME") = DTSSource("LASTNAME")
DTSDestination("EMAIL_ADR") = DTSSource("EMAIL_ADR")
Main = DTSTransformstat_UpdateQuery
End Function
---endcode---


Here is my problem... When the package executes it does fine. but it is
adding/inserting duplicate records in the final table. I only want it to
update those records and insert if a record does not exists.

Is this the right approach? It seems like it should work as intended but
just inserts records when it shoul dbe updating. I am at a loss on this.
Any help would be much appreciated.

--
Joey Durham
www.ultraweaver.com - Tutorials/Downloads
www.geekforum.com





Reply With Quote
  #2  
Old   
Joey D
 
Posts: n/a

Default Re: Insert Update query problem in DTS - 08-11-2004 , 09:15 AM






Maybe it is just the SQL that seems to be out of wack? Does this SQL
statement look right? It is in the "Update" section of my DDQ. It should
update the field in tblContacts if they exist and insert a if it does not.


UPDATE tblContacts
SET CONT_ID = ?, FIRSTNAME = ?, LASTNAME = ?, EMAIL_ADR = ?
WHERE CONT_ID = ? IF EXISTS

(SELECT *
FROM tblContactsScratch
WHERE CONT_ID = ?)

UPDATE tblContacts
SET CONT_ID = ?, FIRSTNAME = ?, LASTNAME = ?, EMAIL_ADR = ?

WHERE (CONT_ID = ?)

ELSE

INSERT INTO
tblContacts(CONT_ID, FIRSTNAME, LASTNAME, EMAIL_ADR)
VALUES (?, ?, ?, ?)

--
Joey Durham
www.ultraweaver.com - Tutorials/Downloads
www.geekforum.com



"Joey D" <joey (AT) ultraweaver (DOT) com> wrote

Quote:
Hi,

I have set up DTS to import a csv file into a scratch table. From here I
have another connection to the final destination table.

But before going to the Final Table I have added a DDQ to check for insert
or update depending on wether the record exists.

Text File ------> Scratch Table ------> Final Table
|
|
|
DDQ

I have the DDQ updating if record in final table exists and inserting if
not. At least it is suppose to.

Here is my query I have in the "Queries" tab for the "Update" of the DDQ
properties...


---start code---
UPDATE tblContacts
SET CONT_ID = ?, FIRSTNAME = ?, LASTNAME = ?, EMAIL_ADR = ?
WHERE CONT_ID = ? IF EXISTS

(SELECT *
FROM tblContactsScratch
WHERE CONT_ID = ?)

UPDATE tblContacts
SET CONT_ID = ?, FIRSTNAME = ?, LASTNAME = ?, EMAIL_ADR = ?

WHERE (CONT_ID = ?)

ELSE

INSERT INTO
tblContacts(CONT_ID, FIRSTNAME, LASTNAME, EMAIL_ADR)
VALUES (?, ?, ?, ?)
---end code---

Here is what my transformation looks like.


---startcode---
'************************************************* *********************
' Visual Basic Transformation Script
'************************************************* ***********************

' Copy each source column to the destination column
Function Main()
' the following will be used as parameters in place of ?
DTSDestination("CONT_ID") = DTSSource("CONT_ID")
DTSDestination("FIRSTNAME") = DTSSource("FIRSTNAME")
DTSDestination("LASTNAME") = DTSSource("LASTNAME")
DTSDestination("EMAIL_ADR") = DTSSource("EMAIL_ADR")
Main = DTSTransformstat_UpdateQuery
End Function
---endcode---


Here is my problem... When the package executes it does fine. but it is
adding/inserting duplicate records in the final table. I only want it to
update those records and insert if a record does not exists.

Is this the right approach? It seems like it should work as intended but
just inserts records when it shoul dbe updating. I am at a loss on this.
Any help would be much appreciated.

--
Joey Durham
www.ultraweaver.com - Tutorials/Downloads
www.geekforum.com






Reply With Quote
  #3  
Old   
Darren Green
 
Posts: n/a

Default Re: Insert Update query problem in DTS - 08-12-2004 , 02:46 PM



You would normally only have the UPDATE statement in the "Update" query
for your DDQ. You would have a single INSERT in the "Insert" query. You
use a lookup to do the SELECT for existence check. You then have an If
construct in the script, something like this-

Function Main()
' the following will be used as parameters in place of
?
DTSDestination("CONT_ID") = DTSSource("CONT_ID")
DTSDestination("FIRSTNAME") = DTSSource("FIRSTNAME")
DTSDestination("LASTNAME") = DTSSource("LASTNAME")
DTSDestination("EMAIL_ADR") = DTSSource("EMAIL_ADR")

If DTSLookups("CheckExistence").Value = 1 Then
Main = DTSTransformstat_UpdateQuery
Else
Main = DTSTransformstat_InsertQuery
End If
End Function


The return value from the transformation function determines which query
is run. Of course you can even assign different values to each query,
and the query purpose doesn't have to match the nominal names either,
but they fit with the classic usage.

--
Darren Green (SQL Server MVP)
DTS - http://www.sqldts.com

PASS - the definitive, global community for SQL Server professionals
http://www.sqlpass.org


Reply With Quote
  #4  
Old   
Joey D
 
Posts: n/a

Default Re: Insert Update query problem in DTS - 08-13-2004 , 09:22 AM



Thank you Darren!

I originally tried this (new to SQL and DTS) and did not get it to work. I
will try again based on the info. BTW, thanks for the site. Has helped me
understand DTS a bit better.

--
Joey Durham
www.ultraweaver.com - Tutorials/Downloads
www.geekforum.com

"Darren Green" <darren.green (AT) reply-to-newsgroup-sqldts (DOT) com> wrote

Quote:
You would normally only have the UPDATE statement in the "Update" query
for your DDQ. You would have a single INSERT in the "Insert" query. You
use a lookup to do the SELECT for existence check. You then have an If
construct in the script, something like this-

Function Main()
' the following will be used as parameters in place of
?
DTSDestination("CONT_ID") = DTSSource("CONT_ID")
DTSDestination("FIRSTNAME") = DTSSource("FIRSTNAME")
DTSDestination("LASTNAME") = DTSSource("LASTNAME")
DTSDestination("EMAIL_ADR") = DTSSource("EMAIL_ADR")

If DTSLookups("CheckExistence").Value = 1 Then
Main = DTSTransformstat_UpdateQuery
Else
Main = DTSTransformstat_InsertQuery
End If
End Function


The return value from the transformation function determines which query
is run. Of course you can even assign different values to each query,
and the query purpose doesn't have to match the nominal names either,
but they fit with the classic usage.

--
Darren Green (SQL Server MVP)
DTS - http://www.sqldts.com

PASS - the definitive, global community for SQL Server professionals
http://www.sqlpass.org




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.