Solved

DTS - Conditional execution of UPDATE & INSERT Queries in single package.

Posted on 2004-09-07
8
508 Views
Last Modified: 2008-01-09
Hi,

       I am new to DTS. How can I implement the following situation in DTS Package.

1. get data from EXCEL and define the key fileds in FILE A,
2. Check for existence of record in FILE A with the above defined key fields.
3. If the record exists in File A, Then Update.
    Else, Insert

I have 2 different Queries for Update & Insert for File A. But, don't know how & where to put condition to check record existence.

Can anyone help me??  

0
Comment
Question by:newcom408
  • 4
  • 4
8 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 11999713
Personally, I would insert the contents of the Excel file into a temp table and then issue simple update/insert statements.  If you don't want to do this, you can use the Data Driven Query (DDQ) task of DTS to load the data with insert/update conditions.

Brett
0
 
LVL 34

Expert Comment

by:arbert
ID: 11999735
So, if you insert the excel contents into a table called Staging it would look something like this in your execute SQL task:

update yourtable
set col1=staging.col1, col2=staging.col2
from yourtable inner join staging
on yourtable.key=staging.key


insert into your table (co1,col2)
select col1,col2 from staging left join yourtable on staging.key=yourtable.key
where yourtable.key is null
0
 

Author Comment

by:newcom408
ID: 12000428
I did the same. but, the problem is i need to derive the keys by using couple of other tables. So, some how i am not able to insert with the following query.... If I remove the 'F' part, then i am able to insert. but, if i don't use that condition, i will end up with duplicate records.


INSERT INTO   CUSTOMER_FACT
(CUST_KEY,COUNTRY_KEY,CURRENCY_KEY,CALENDAR_KEY,  
GROSS_SALES_LOCAL, GROSS_SALES_US)
SELECT A.CUST_KEY,B.COUNTRY_KEY,C.CURRENCY_KEY,D.CALENDAR_KEY,  
E.GROSS_SALES_LOCAL, E.GROSS_SALES_LOCAL * C.RATE
FROM         CUSTOMER A, COUNTRY B, CURRENCY_CONVERSION_RATES C, CALENDAR_DATE D, CUSTOMER_STAGING E, CUSTOMER_FACT F
WHERE     A.CUST_NAME = E.CUST_NAME AND
B.COUNTRY_NAME = E.COUNTRY_NAME AND
B.COUNTRY_CURRENCY_CODE = C.FROM_CURRENCY_CODE AND
C.CURRENCY_CONVERSION_FISCAL_YEA = D.YEAR AND
C.CURRENCY_CONVERSION_MONTH = E.MONTH AND
C.CURRENCY_CONVERSION_TYPE  LIKE 'YTD'  AND
D.YEAR = E.YEAR AND
D.MONTH_DESC = E.MONTH_DESC AND


F.CUST_KEY IS NULL AND
F.COUNTRY_KEY IS NULL AND
F.CURRENCY_KEY IS NULL AND
F.CALENDAR_KEY IS NULL
0
 
LVL 34

Expert Comment

by:arbert
ID: 12000754
So do you not return records when you remove the Customer_FACT?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:newcom408
ID: 12000865
Yes i am able to insert but, when we get the same records in next load from EXCEL, this query will insert them as well with out any check for existence of records. that means we will have duplicates. because all those keys are foreign keys.

Here is the sequence I am following:

1. Delete Staging file
On Success,
2. import EXCEL in to Staging file
On Success,
3. Update FACT File if the record not exists.
On Success,
4. Insert records in to FACT file if the record not exists.
0
 

Author Comment

by:newcom408
ID: 12000896
correction,

3. Update FACT File if the record exists.
0
 

Author Comment

by:newcom408
ID: 12000907
Acually,

my question is, how can i check for the existence of record before UPDATE/INSERT??
0
 
LVL 34

Accepted Solution

by:
arbert earned 250 total points
ID: 12007551
"my question is, how can i check for the existence of record before UPDATE/INSERT??"

Look at my post above dated "Date: 09/07/2004 12:45PM MDT".  You will have an insert statement and an update statement.  Both will utilize the existing table to check for the existence of the record.  You can also use the DDQ task...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now