Solved

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

Posted on 2004-09-07
8
510 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 34

Expert Comment

by:arbert
ID: 12000754
So do you not return records when you remove the Customer_FACT?
0
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

837 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