SELECT
o.obj_code as equipmentcode,
o.obj_org as equiporganization,
o.obj_desc as equipmentdesc,
lev1 as level1,
lev1org as level1org,
lev2 as level2,
lev2org as level2org,
lev3 as level3,
lev3org as level3org,
lev3a as level3a,
lev3aorg as level3aorg,
lev4 as level4,
lev4org as level4org
FROM r5userorganization, r5objects o,(SELECT stc_child equip, stc_child_org equiporg, stc_parent lev1, stc_parent_org lev1org, NULL lev2, NULL lev2org,
NULL lev3, NULL lev3org, NULL lev4, NULL lev4org, NULL lev3a, NULL lev3aorg
FROM   r5level1, r5structures
WHERE  stc_parent     = lv1_code
AND    stc_parent_org = lv1_org
AND    lv1_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv1_notused = '-'
AND    COALESCE( lv1_code, 'x' ) = COALESCE( <param.level1>, lv1_code, 'x' )
AND    COALESCE( lv1_org, 'x' )  = COALESCE( <param.level1_org>, lv1_org, 'x' )
AND    <param.level2>  IS NULL
AND    <param.level3>  IS NULL
AND    <param.level4>  IS NULL
AND    <param.level3a> IS NULL
UNION
SELECT stc_child, stc_child_org, lv2_level1, lv2_level1_org, stc_parent, stc_parent_org, NULL, NULL, NULL, NULL, NULL, NULL
FROM   r5level2, r5structures
WHERE  stc_parent     = lv2_code
AND    stc_parent_org = lv2_org
AND    lv2_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv2_notused = '-'
AND    COALESCE( lv2_code, 'x' )       = COALESCE( <param.level2>, lv2_code, 'x' )
AND    COALESCE( lv2_org, 'x' )        = COALESCE( <param.level2_org>, lv2_org, 'x' )
AND    COALESCE( lv2_level1, 'x' )     = COALESCE( <param.level1>, lv2_level1, 'x' )
AND    COALESCE( lv2_level1_org, 'x' ) = COALESCE( <param.level1_org>, lv2_level1_org, 'x' )
AND    <param.level3>  IS NULL
AND    <param.level4>  IS NULL
AND    <param.level3a> IS NULL
UNION
SELECT stc_child, stc_child_org, lv3_level1, lv3_level2_org, lv3_level2, lv3_level2_org, stc_parent, stc_parent_org, NULL, NULL, NULL, NULL
FROM   r5level3, r5structures
WHERE  stc_parent     = lv3_code
AND    stc_parent_org = lv3_org
AND    lv3_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv3_notused = '-'
AND    COALESCE( lv3_code, 'x' )       = COALESCE( <param.level3>, lv3_code, 'x' )
AND    COALESCE( lv3_org, 'x' )        = COALESCE( <param.level3_org>, lv3_org, 'x' )
AND    COALESCE( lv3_level1, 'x' )     = COALESCE( <param.level1>, lv3_level1, 'x' )
AND    COALESCE( lv3_level1_org, 'x' ) = COALESCE( <param.level1_org>, lv3_level1_org, 'x' )
AND    COALESCE( lv3_level2, 'x' )     = COALESCE( <param.level2>, lv3_level2, 'x' )
AND    COALESCE( lv3_level2_org, 'x' ) = COALESCE( <param.level2_org>, lv3_level2_org, 'x' )
AND    <param.level4>  IS NULL
UNION
SELECT stc_child, stc_child_org, l3a_level1, l3a_level2_org, l3a_level2, l3a_level2_org, NULL, NULL, NULL, NULL, stc_parent, stc_parent_org
FROM   r5level3a, r5structures
WHERE  stc_parent     = l3a_code
AND    stc_parent_org = l3a_org
AND    l3a_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    l3a_notused = '-'
AND    COALESCE( l3a_code, 'x' )       = COALESCE( <param.level3a>, l3a_code, 'x' )
AND    COALESCE( l3a_org, 'x' )        = COALESCE( <param.level3a_org>, l3a_org, 'x' )
AND    COALESCE( l3a_level1, 'x' )     = COALESCE( <param.level1>, l3a_level1, 'x' )
AND    COALESCE( l3a_level1_org, 'x' ) = COALESCE( <param.level1_org>, l3a_level1_org, 'x' )
AND    COALESCE( l3a_level2, 'x' )     = COALESCE( <param.level2>, l3a_level2, 'x' )
AND    COALESCE( l3a_level2_org, 'x' ) = COALESCE( <param.level2_org>, l3a_level2_org, 'x' )
AND    <param.level3> IS NULL
AND    <param.level4> IS NULL
UNION
SELECT stc_child, stc_child_org, lv4_level1, lv4_level2_org, lv4_level2, lv4_level2_org, lv4_level3, lv4_level3_org, stc_parent, stc_parent_org, NULL, NULL
FROM   r5level4, r5structures
WHERE  stc_parent     = lv4_code
AND    stc_parent_org = lv4_org
AND    lv4_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv4_notused = '-'
AND    COALESCE( lv4_code, 'x' )       = COALESCE( <param.level4>, lv4_code, 'x' )
AND    COALESCE( lv4_org, 'x' )        = COALESCE( <param.level4_org>, lv4_org, 'x' )
AND    COALESCE( lv4_level1, 'x' )     = COALESCE( <param.level1>, lv4_level1, 'x' )
AND    COALESCE( lv4_level1_org, 'x' ) = COALESCE( <param.level1_org>, lv4_level1_org, 'x' )
AND    COALESCE( lv4_level2, 'x' )     = COALESCE( <param.level2>, lv4_level2, 'x' )
AND    COALESCE( lv4_level2_org, 'x' ) = COALESCE( <param.level2_org>, lv4_level2_org, 'x' )
AND    COALESCE( lv4_level3, 'x' )     = COALESCE( <param.level3>, lv4_level3, 'x' )
AND    COALESCE( lv4_level3_org, 'x' ) = COALESCE( <param.level3_org>, lv4_level3_org, 'x' )
AND    <param.level3a> IS NULL)
WHERE  obj_code = equip
AND    obj_org  = equiporg
AND    obj_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    COALESCE( obj_notused, '-' ) <> '+'
AND    obj_org = uog_org
AND    uog_user = <parameter.r5user>
AND    ( uog_common  = '+' OR uog_org =  <control.org>  )
AND (<param.bypassdeptsecurity> = 'true' OR (<parameter.deptsec> ='OFF' OR
(obj_mrc IN( SELECT dse_mrc FROM r5departmentsecurity WHERE dse_user =<parameter.r5user> AND dse_mrc = obj_mrc AND COALESCE(dse_readonly, '-') = '-')))) ORDER BY equip ASC, o.obj_org ASC

