__IF_EXISTS__
SELECT 1
  FROM CI_UT_INSTL_DTL
WHERE SCRIPT_FILE_NAME = 'FW43050_POSTDATA_OUAF-3027.SQL'
   AND END_DTTM IS NOT NULL
/
__THEN__
__EXIT__
__ENDIF__

__IF_EXISTS__
SELECT 1
  FROM CI_INSTALL_PROD
WHERE INSTALL_OPT_ID = '11111'
   AND OWNER_FLG = 'F1'
/
__ELSE__
__EXIT__
__ENDIF__
 
INSERT INTO CI_MD_MO_OPT (MAINT_OBJ_CD, MAINT_OBJ_OPT_FLG, MAINT_OBJ_OPT_VAL, SEQ_NUM, VERSION, OWNER_FLG)
SELECT DISTINCT B.MAINT_OBJ_CD, 'F1BP', A.BUS_OBJ_OPT_FLG,
              NVL((Select max(C.SEQ_NUM)+1 from CI_MD_MO_OPT C where C.MAINT_OBJ_CD = B.MAINT_OBJ_CD and C.MAINT_OBJ_OPT_FLG = 'F1BP'),1) as SEQ_NUM, 1, 'CM'
FROM F1_BUS_OBJ_OPT A,
             F1_BUS_OBJ B
WHERE A.BUS_OBJ_OPT_FLG IN ('F1BO', 'F1DA', 'F1EP', 'F1ET', 'F1KX', 'F1MQ', 'F1PS', 'F1RC', 'F1SY', 'F1SZ')
      AND A.BUS_OBJ_CD = B.BUS_OBJ_CD
AND NOT EXISTS
(SELECT 'X' FROM CI_MD_MO_OPT Z 
 WHERE Z.MAINT_OBJ_CD = B.MAINT_OBJ_CD 
      AND Z.MAINT_OBJ_OPT_FLG = 'F1BP' 
      AND Z.MAINT_OBJ_OPT_VAL = A.BUS_OBJ_OPT_FLG)
group by B.MAINT_OBJ_CD, A.BUS_OBJ_OPT_FLG
/