Solved

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

Posted on 2004-09-07
8
511 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

735 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