BEGIN TRY
    BEGIN TRANSACTION;

    /* =========================================
    1. Work Order - Asset Group Code
    ========================================= */
    SELECT 
        section_name          = '1. Work Order - Asset Group Code',
        d.site_cd,
        detail_value_original = d.wko_mst_asset_group_code,
        master_value_original = m.ast_grp_grp_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN ast_grp m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_asset_group_code) = RTRIM(m.ast_grp_grp_cd)
    WHERE d.wko_mst_asset_group_code IS NOT NULL
      AND d.wko_mst_asset_group_code LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_asset_group_code = (m.ast_grp_grp_cd)
    --FROM wko_mst d
    --INNER JOIN ast_grp m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_asset_group_code) = RTRIM(m.ast_grp_grp_cd)
    --WHERE d.wko_mst_asset_group_code IS NOT NULL
    --  AND d.wko_mst_asset_group_code LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_asset_group_code) <> DATALENGTH(RTRIM(m.ast_grp_grp_cd));

    /* =========================================
    2. Work Order - Asset Level
    ========================================= */
    SELECT 
        section_name          = '2. Work Order - Asset Level',
        d.site_cd,
        detail_value_original = d.wko_mst_asset_level,
        master_value_original = m.ast_lvl_ast_lvl,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN ast_lvl m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_asset_level) = RTRIM(m.ast_lvl_ast_lvl)
    WHERE d.wko_mst_asset_level IS NOT NULL
      AND d.wko_mst_asset_level LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_asset_level = (m.ast_lvl_ast_lvl)
    --FROM wko_mst d
    --INNER JOIN ast_lvl m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_asset_level) = RTRIM(m.ast_lvl_ast_lvl)
    --WHERE d.wko_mst_asset_level IS NOT NULL
    --  AND d.wko_mst_asset_level LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_asset_level) <> DATALENGTH(RTRIM(m.ast_lvl_ast_lvl));

    /* =========================================
    3. Work Order - Asset Location
    ========================================= */
    SELECT 
        section_name          = '3. Work Order - Asset Location',
        d.site_cd,
        detail_value_original = d.wko_mst_asset_location,
        master_value_original = m.ast_loc_ast_loc,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN ast_loc m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_asset_location) = RTRIM(m.ast_loc_ast_loc)
    WHERE d.wko_mst_asset_location IS NOT NULL
      AND d.wko_mst_asset_location LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_asset_location = (m.ast_loc_ast_loc)
    --FROM wko_mst d
    --INNER JOIN ast_loc m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_asset_location) = RTRIM(m.ast_loc_ast_loc)
    --WHERE d.wko_mst_asset_location IS NOT NULL
    --  AND d.wko_mst_asset_location LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_asset_location) <> DATALENGTH(RTRIM(m.ast_loc_ast_loc));

    /* =========================================
    4. Work Order - Charge Cost Center
    ========================================= */
    SELECT 
        section_name          = '4. Work Order - Charge Cost Center',
        d.site_cd,
        detail_value_original = d.wko_mst_chg_costcenter,
        master_value_original = m.costcenter,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN cf_cost_center m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_chg_costcenter) = RTRIM(m.costcenter)
    WHERE d.wko_mst_chg_costcenter IS NOT NULL
      AND d.wko_mst_chg_costcenter LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_chg_costcenter = (m.costcenter)
    --FROM wko_mst d
    --INNER JOIN cf_cost_center m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_chg_costcenter) = RTRIM(m.costcenter)
    --WHERE d.wko_mst_chg_costcenter IS NOT NULL
    --  AND d.wko_mst_chg_costcenter LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_chg_costcenter) <> DATALENGTH(RTRIM(m.costcenter));

    /* =========================================
    5. Work Order - Work Area
    ========================================= */
    SELECT 
        section_name          = '5. Work Order - Work Area',
        d.site_cd,
        detail_value_original = d.wko_mst_work_area,
        master_value_original = m.mst_war_work_area,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN mst_war m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_work_area) = RTRIM(m.mst_war_work_area)
    WHERE d.wko_mst_work_area IS NOT NULL
      AND d.wko_mst_work_area LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_work_area = (m.mst_war_work_area)
    --FROM wko_mst d
    --INNER JOIN mst_war m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_work_area) = RTRIM(m.mst_war_work_area)
    --WHERE d.wko_mst_work_area IS NOT NULL
    --  AND d.wko_mst_work_area LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_work_area) <> DATALENGTH(RTRIM(m.mst_war_work_area));

    /* =========================================
    6. Work Order - Original Priority
    ========================================= */
    SELECT 
        section_name          = '6. Work Order - Original Priority',
        d.site_cd,
        detail_value_original = d.wko_mst_orig_priority,
        master_value_original = m.wrk_pri_pri_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN wrk_pri m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_orig_priority) = RTRIM(m.wrk_pri_pri_cd)
    WHERE d.wko_mst_orig_priority IS NOT NULL
      AND d.wko_mst_orig_priority LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_orig_priority = (m.wrk_pri_pri_cd)
    --FROM wko_mst d
    --INNER JOIN wrk_pri m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_orig_priority) = RTRIM(m.wrk_pri_pri_cd)
    --WHERE d.wko_mst_orig_priority IS NOT NULL
    --  AND d.wko_mst_orig_priority LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_orig_priority) <> DATALENGTH(RTRIM(m.wrk_pri_pri_cd));

    /* =========================================
    7. Work Order - Plan Priority
    ========================================= */
    SELECT 
        section_name          = '7. Work Order - Plan Priority',
        d.site_cd,
        detail_value_original = d.wko_mst_plan_priority,
        master_value_original = m.wrk_pri_pri_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN wrk_pri m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_plan_priority) = RTRIM(m.wrk_pri_pri_cd)
    WHERE d.wko_mst_plan_priority IS NOT NULL
      AND d.wko_mst_plan_priority LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_plan_priority = (m.wrk_pri_pri_cd)
    --FROM wko_mst d
    --INNER JOIN wrk_pri m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_plan_priority) = RTRIM(m.wrk_pri_pri_cd)
    --WHERE d.wko_mst_plan_priority IS NOT NULL
    --  AND d.wko_mst_plan_priority LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_plan_priority) <> DATALENGTH(RTRIM(m.wrk_pri_pri_cd));

    /* =========================================
    8. Work Order - Work Type
    ========================================= */
    SELECT 
        section_name          = '8. Work Order - Work Type',
        d.site_cd,
        detail_value_original = d.wko_det_work_type,
        master_value_original = m.wrk_typ_typ_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_det d
    INNER JOIN wrk_typ m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_det_work_type) = RTRIM(m.wrk_typ_typ_cd)
    WHERE d.wko_det_work_type IS NOT NULL
      AND d.wko_det_work_type LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_det_work_type = (m.wrk_typ_typ_cd)
    --FROM wko_det d
    --INNER JOIN wrk_typ m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_det_work_type) = RTRIM(m.wrk_typ_typ_cd)
    --WHERE d.wko_det_work_type IS NOT NULL
    --  AND d.wko_det_work_type LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_det_work_type) <> DATALENGTH(RTRIM(m.wrk_typ_typ_cd));

    /* =========================================
    9. Work Order - Work Group
    ========================================= */
    SELECT 
        section_name          = '9. Work Order - Work Group',
        d.site_cd,
        detail_value_original = d.wko_det_work_grp,
        master_value_original = m.wrk_grp_grp_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_det d
    INNER JOIN wrk_grp m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_det_work_grp) = RTRIM(m.wrk_grp_grp_cd)
    WHERE d.wko_det_work_grp IS NOT NULL
      AND d.wko_det_work_grp LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_det_work_grp = RTRIM(m.wrk_grp_grp_cd)
    --FROM wko_det d
    --INNER JOIN wrk_grp m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_det_work_grp) = RTRIM(m.wrk_grp_grp_cd)
    --WHERE d.wko_det_work_grp IS NOT NULL
    --  AND d.wko_det_work_grp LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_det_work_grp) <> DATALENGTH(RTRIM(m.wrk_grp_grp_cd));

    /* =========================================
    10. Work Order - Fault Code
    ========================================= */
    SELECT 
        section_name          = '10. Work Order - Fault Code',
        d.site_cd,
        detail_value_original = d.wko_mst_flt_code,
        master_value_original = m.wrk_flt_fault_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_mst d
    INNER JOIN wrk_flt m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_mst_flt_code) = RTRIM(m.wrk_flt_fault_cd)
    WHERE d.wko_mst_flt_code IS NOT NULL
      AND d.wko_mst_flt_code LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_mst_flt_code = (m.wrk_flt_fault_cd)
    --FROM wko_mst d
    --INNER JOIN wrk_flt m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_mst_flt_code) = RTRIM(m.wrk_flt_fault_cd)
    --WHERE d.wko_mst_flt_code IS NOT NULL
    --  AND d.wko_mst_flt_code LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_mst_flt_code) <> DATALENGTH(RTRIM(m.wrk_flt_fault_cd));

    /* =========================================
    11. Work Order - Work Class
    ========================================= */
    SELECT 
        section_name          = '11. Work Order - Work Class',
        d.site_cd,
        detail_value_original = d.wko_det_work_class,
        master_value_original = m.wrk_cls_cls_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_det d
    INNER JOIN wrk_cls m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_det_work_class) = RTRIM(m.wrk_cls_cls_cd)
    WHERE d.wko_det_work_class IS NOT NULL
      AND d.wko_det_work_class LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_det_work_class = (m.wrk_cls_cls_cd)
    --FROM wko_det d
    --INNER JOIN wrk_cls m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_det_work_class) = RTRIM(m.wrk_cls_cls_cd)
    --WHERE d.wko_det_work_class IS NOT NULL
    --  AND d.wko_det_work_class LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_det_work_class) <> DATALENGTH(RTRIM(m.wrk_cls_cls_cd));

    /* =========================================
    12. Work Order - Cause Code
    ========================================= */
    SELECT 
        section_name          = '12. Work Order - Cause Code',
        d.site_cd,
        detail_value_original = d.wko_det_cause_code,
        master_value_original = m.wrk_ccd_cause_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_det d
    INNER JOIN wrk_ccd m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_det_cause_code) = RTRIM(m.wrk_ccd_cause_cd)
    WHERE d.wko_det_cause_code IS NOT NULL
      AND d.wko_det_cause_code LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_det_cause_code = (m.wrk_ccd_cause_cd)
    --FROM wko_det d
    --INNER JOIN wrk_ccd m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_det_cause_code) = RTRIM(m.wrk_ccd_cause_cd)
    --WHERE d.wko_det_cause_code IS NOT NULL
    --  AND d.wko_det_cause_code LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_det_cause_code) <> DATALENGTH(RTRIM(m.wrk_ccd_cause_cd));

    /* =========================================
    13. Work Order - Action Code
    ========================================= */
    SELECT 
        section_name          = '13. Work Order - Action Code',
        d.site_cd,
        detail_value_original = d.wko_det_act_code,
        master_value_original = m.wrk_act_action_cd,
        d.mbl_audit_date,
        d.mbl_audit_user
    FROM wko_det d
    INNER JOIN wrk_act m
        ON d.site_cd = m.site_cd
       AND RTRIM(d.wko_det_act_code) = RTRIM(m.wrk_act_action_cd)
    WHERE d.wko_det_act_code IS NOT NULL
      AND d.wko_det_act_code LIKE '% '
      AND d.mbl_audit_date IS NOT NULL
      AND d.mbl_audit_user IS NOT NULL;

    --UPDATE d
    --   SET d.wko_det_act_code = (m.wrk_act_action_cd)
    --FROM wko_det d
    --INNER JOIN wrk_act m
    --    ON d.site_cd = m.site_cd
    --   AND RTRIM(d.wko_det_act_code) = RTRIM(m.wrk_act_action_cd)
    --WHERE d.wko_det_act_code IS NOT NULL
    --  AND d.wko_det_act_code LIKE '% '
    --  AND d.mbl_audit_date IS NOT NULL
    --  AND d.mbl_audit_user IS NOT NULL
    --  AND DATALENGTH(d.wko_det_act_code) <> DATALENGTH(RTRIM(m.wrk_act_action_cd));

    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;

    SELECT
        ERROR_NUMBER()    AS ErrorNumber,
        ERROR_MESSAGE()   AS ErrorMessage,
        ERROR_LINE()      AS ErrorLine,
        ERROR_PROCEDURE() AS ErrorProcedure;
END CATCH;
GO