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?
 
Pawan KumarConnect With a Mentor Database 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
 
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
 
mehul bhaktaAuthor Commented:
Pawan , you're the man!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.