Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Question on MERGE into inside a stored procedure?

user11961230Jan 26 2011 — edited Jan 26 2011
Hello Folks,
I have this procedure which actually has a MERGE command inside the procedure and basically all it does is it inserts all the data from the temp table when there is no match on the Bill_ID.
Here my requirement is on a daily basis the Bill_ID can be assigned to a different Event_ID. If for example the Bill_ID has changed its event tommorrow the procedure should update the row with the new Event_ID. But in my procedure its not updating because the Bill_ID hasnt changed and its the same(coz am using MERGE insert when not matched). How do i acheive this on a day to day basis. What is the building block of code to do this. Am kinda new to programming and am trying to learn.

CREATE OR REPLACE PROCEDURE load_fadm_staging_area_test (
  p_data_load_date DATE
) IS
  v_start_date   DATE;
  v_end_date     DATE;
BEGIN
  SELECT NVL (p_data_load_date, SYSDATE) - 7,
         NVL (p_data_load_date, SYSDATE) - 1
  INTO   v_start_date,
         v_end_date
  FROM   DUAL;
 
  MERGE INTO stg_fadm_hri_stage_bill_test b
  USING      (SELECT *
              FROM   stage_bill
              WHERE  created_date BETWEEN v_start_date AND v_end_date) a
  ON         (b.bill_id = a.bill_id,)
  WHEN NOT MATCHED THEN
    INSERT     (batch_id,
                beginning_service_date,
                bill_id,
                bill_method,
                bill_number,
                bill_received_date,
                bill_status,
                bill_type,
                change_oltp_by,
                change_oltp_date,
                client_datafeed_code,
                client_id,
                created_date,
                date_of_incident,
                date_paid,
                deleted_oltp_by,
                deleted_oltp_date,
                duplicate_bill,
                ending_service_date,
                event_case_id,
                event_id,
                from_oltp_by,
                oltp_bill_status,
                review_status,
                row_effective_date,
                row_end_date
               )
    VALUES     (a.batch_id,
                a.beginning_service_date,
                a.bill_id,
                a.bill_method,
                a.bill_number,
                a.bill_received_date,
                a.bill_status,
                a.bill_type,
                a.change_oltp_by,
                a.change_oltp_date,
                a.client_datafeed_code,
                a.client_id,
                a.created_date,
                a.date_of_incident,
                a.date_paid,
                a.deleted_oltp_by,
                a.deleted_oltp_date,
                a.duplicate_bill,
                a.ending_service_date,
                a.event_case_id,
                a.event_id,
                a.from_oltp_by,
                a.oltp_bill_status,
                a.review_status,
                v_start_date,
                v_end_date
               );
END load_fadm_staging_area_test;
Edited by: user11961230 on Jan 26, 2011 9:36 AM
This post has been answered by John Spencer on Jan 26 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2011
Added on Jan 26 2011
11 comments
7,502 views