SELECT
o.obj_code as equipmentcode,
o.obj_org as equiporganization,
o.obj_desc as equipmentdesc,
o.obj_obtype as equipmenttype,
o.obj_obrtype as equipmentrtype

FROM  R5MRCS m, R5USERORGANIZATION u, R5OBJECTS o
WHERE COALESCE(o.obj_notused, '-') <> '+'
AND o.obj_obrtype = <param.objectrtype>
AND o.obj_mrc = m.mrc_code
AND u.uog_user =<parameter.r5user>
AND u.uog_org =  o.obj_org
AND (u.uog_org = <param.objectorg> OR EXISTS (SELECT 1 FROM r5organization WHERE org_code = <param.objectorg> AND org_common = '+'))
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, '-') = '-'))))
AND (o.obj_code||'#'||o.obj_org) <> (<param.objectcode>||'#'||<param.objectorg>)
AND (o.obj_code||'#'||o.obj_org) NOT IN
( WITH RECURSIVE t ( stc_parent, stc_parent_org, stc_child, stc_child_org ) AS (
    SELECT stc_parent, stc_parent_org, stc_child, stc_child_org
    FROM   r5structures
    WHERE  stc_parent = <param.objectcode>
    AND    stc_parent_org = <param.objectorg>
    UNION
    SELECT s.stc_parent, s.stc_parent_org, s.stc_child, s.stc_child_org
    FROM   r5structures s
           JOIN t on s.stc_parent = t.stc_child AND s.stc_parent_org = t.stc_child_org )
  SELECT stc_child||'#'||stc_child_org
  FROM   t )
AND NOT EXISTS ( SELECT  1 FROM R5INSTALL WHERE 'P' = o.obj_obrtype AND INS_CODE = 'OPT_TAGS' AND INS_DESC = '+'
AND EXISTS (SELECT 1 FROM R5OBJECTS a WHERE a.OBJ_POSITION = o.obj_code AND a.obj_position_org = o.OBJ_ORG AND a.obj_obrtype = 'A'
AND a.obj_rstatus = 'I' ))