Solved

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

Posted on 2004-09-07
8
507 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

760 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