oracle SQL - update, insert or insert into another table

hello,

I'm using PL/SQL developer using 11g.

My problem: I have a table that contains new records 'load_customer_data' and I've compared them to another table 'customer_data'. I've determined what do to when there is a match or if there isn't by a field called 'what_to_do'. in that field it says if I should update a record in 'customer_data' from 'load_customer_data' or insert a record from  'load_customer_data' into 'customer_data' and if the records are the same in both tables to say its an exception.

a merge statement would take care of the update or insert but I can't think of a way to say if 'what_to_do' = 'exception' then insert it into 'customer_data_exceptions'

my manager said use and else if statement but I can't seem to get that to work neither.

Any thoughts?

Thanks
mehul bhaktaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
can you share few sample from the input tables and the expected output in the destination table.

In this if you do no want to use MERGE then we can use IF with INSERT / UPDATE statements.
0
mehul bhaktaAuthor Commented:
Pawan my man, how you doing? thanks for getting back to me. please find attached a spreadsheet with a sample of what I want my script to do
asking-for-help.xlsx
0
Pawan KumarDatabase ExpertCommented:
Please try these-

/*Update customer_data  from load_customer_data  where WHAT_TO_DO = 'update' , You can also use UPDATE Statement instead of merge*/
MERGE INTO customer_data c
USING load_customer_data l ON c.ICE_CUSTOMER_ID = l.ICE_CUSTOMER_ID AND l.WHAT_TO_DO = 'update'
WHEN MATCHED THEN UPDATE 
	SET 
	 c.FROM_DATE = l.FROM_DATE
	,c.TO_DATE = l.TO_DATE
	,c.CUSTOMER_START_DATE = l.CUSTOMER_START_DATE
	,c.DATE_OF_BIRTH = l.DATE_OF_BIRTH
	,c.GENDER = l.GENDER
	,c.VULNERABILITY = l.VULNERABILITY
	,c.PAYMENT_METHOD_GROUP =l.PAYMENT_METHOD_GROUP
	,c.CUSTOMER_TYPE = l.CUSTOMER_TYPE;

Open in new window

      

/*Insert the data into customer data where the What_to_do column = Insert */
INSERT INTO customer_data (FROM_DATE,TO_DATE,ICE_CUSTOMER_ID,CUSTOMER_START_DATE,DATE_OF_BIRTH,GENDER,VULNERABILITY,PAYMENT_METHOD_GROUP,CUSTOMER_TYPE)
SELECT FROM_DATE,TO_DATE,ICE_CUSTOMER_ID,CUSTOMER_START_DATE,DATE_OF_BIRTH,GENDER,VULNERABILITY,PAYMENT_METHOD_GROUP,CUSTOMER_TYPE
FROM load_customer_data WHERE WHAT_TO_DO = 'insert'

Open in new window


/*insert the exception data into the exceptions table*/
INSERT INTO load_customer_exceptions 
SELECT FROM_DATE,TO_DATE,ICE_CUSTOMER_ID,CUSTOMER_START_DATE,DATE_OF_BIRTH,GENDER,VULNERABILITY,PAYMENT_METHOD_GROUP,CUSTOMER_TYPE
FROM load_customer_data WHERE What_to_do <> 'exception'

Open in new window


/*truncate load customer table*/
TRUNCATE TABLE load_customer_data;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mehul bhaktaAuthor Commented:
Pawan , you're the man!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.