I have 5 tables in total. I wrote the initial sub query to gather the proper Pace IDs/Product IDs according to the Master Product ID. However, when I go to join to the Status/Record Tables in my procedure, I am getting errors.
- The Master Table has the PACE ID.
- The Pace Table has the Pace ID/Product ID:
| PACE ID | PRODUCT ID |
|---|---|
| 12345 | 776756 |
| 12347 | 776758 |
- Master Pace Mapping Table has the Product ID/Master Product ID:
| Product ID | Master Product ID |
|---|---|
| 776756 | 112987 |
| 776758 | 112987 |
The Status Table has the PACE ID/STATUS/ACCOUNT ID:
| PACE ID | STATUS | ACCOUNT ID |
|---|---|---|
| 12345 | SUBMITTED | A4562075 |
| 12345 | SUBMITTED | A7653082 |
| 12345 | NOT SUBMITTED | A2340563 |
| 12347 | SUBMITTED | A3782257 |
The Record table is the table we need to delete from which has the Pace ID/ Account ID / TO FILE columns:
| PACE ID | ACCOUNT ID | TO FILE |
|---|---|---|
| 12345 | A4562075 | Y |
| 12345 | A7653082 | Y |
| 12345 | A2340563 | Y |
| 12347 | A3782257 | Y |
I need to gather the product IDs using the below sub query which joins to the Master ID, see if the status for each account/product id is Submitted or Not Submitted, then delete from the Record table if we already had submitted.
I am getting syntax errors when trying to incorporate everything within the procedure and am unsure how to proceed. Would anyone be able to explain what I would need to do?
Sub Query that works correctly:
select ma.pace_id, cc.product_id
from prod.master ma
join prod.pace cc on cc.pace_id = ma.pace_id
where cc.product_id in(
select rcm.product_id
from prod.master c
join prod.pace ccm on ccm.pace_id = c.pace_id
join prod.master_pace_mapping rcm on rcm.master_product_id = ccm.product_id and rcm.delete_date is null
where ccm.pace_id = 12345
);
Within the Procedure that is not working:
CREATE OR REPLACE PROCEDURE “PROD”.”PARTICIPATION”
(P_PACE_ID NUMBER,
P_CLIENT VARCHAR2)
MERGE INTO FRT.RECORD
USING(
select ma.pace_id, cc.product_id
from prod.master ma
join prod.pace cc on cc.pace_id = ma.pace_id
where cc.product_id in(
select rcm.product_id
from prod.master c
join prod.pace ccm on ccm.pace_id = c.pace_id
join prod.master_pace_mapping rcm on rcm.master_product_id = ccm.product_id and rcm.delete_date is null
where ccm.pace_id = P_PACE_ID
) A JOIN SELECT S.ACCOUNT_ID, S.STATUS FROM PROD.STATUS S WHERE S.PACE_ID = A.CASE_ID;
WHEN STATUS = ‘SUBMITTED’ THEN DELETE FROM PROD.RECORD