Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-07
8
Medium Priority
?
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

722 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