select 
obj_address as OBJ_ADDRESS,
obj_asmlevel as OBJ_ASMLEVEL,
obj_bin as OBJ_BIN,
obj_category as OBJ_CATEGORY,
obj_criticality as OBJ_CIRTICALITY,
O.OBJ_CLASS as OBJ_CLASS,
O.OBJ_CLASS_ORG as OBJ_CLASS_ORG,
obj_code as OBJ_CODE,
obj_commiss as OBJ_COMMISS,
obj_complevel as OBJ_COMPLEVEL,
obj_costcode as OBJ_COSTCODE,
obj_desc as OBJ_DESC,
obj_dormend as OBJ_DORMEND,
obj_dormstart as OBJ_DORMSTART,
o.obj_location as OBJ_LOCATION,
obj_lot as OBJ_LOT,
obj_manufact as OBJ_MANUFACT,
obj_manufactmodel as OBJ_MANUFACTMODEL,
o.obj_mrc as OBJ_MRC,
obj_notused as OBJ_NOTUSED,
O.OBJ_OBTYPE as OBJ_OBTYPE,
O.OBJ_OBRTYPE as OBJ_OBRTYPE,
o.obj_org as OBJ_ORG,
obj_part as OBJ_PART,
obj_person as OBJ_PERSON,
obj_safety as OBJ_SAFETY,
obj_serialno as OBJ_SERIALNO,
o.obj_status as OBJ_STATUS,
( SELECT UCO_DESC
  FROM r5ucodes
  WHERE uco_rentity = 'OBST' 
  AND
  UCO_CODE = OBJ_STATUS 
)  as OBJ_STATUS_DESC,
obj_store as OBJ_STORE,
obj_syslevel as OBJ_SYSLEVEL,
obj_value as OBJ_VALUE,
obj_xcoordinate as OBJ_XCOORDINATE,
obj_ycoordinate as OBJ_YCOORDINATE,
obj_udfchar01 as OBJ_UDFCHAR01,
obj_udfchar02 as OBJ_UDFCHAR02,
obj_udfchar03 as OBJ_UDFCHAR03,
obj_udfchar04 as OBJ_UDFCHAR04,
obj_udfchar05 as OBJ_UDFCHAR05,
obj_udfchar06 as OBJ_UDFCHAR06,
obj_udfchar07 as OBJ_UDFCHAR07,
obj_udfchar08 as OBJ_UDFCHAR08,
obj_udfchar09 as OBJ_UDFCHAR09,
obj_udfchar10 as OBJ_UDFCHAR10,
obj_udfchar11 as OBJ_UDFCHAR11,
obj_udfchar12 as OBJ_UDFCHAR12,
obj_udfchar13 as OBJ_UDFCHAR13,
obj_udfchar14 as OBJ_UDFCHAR14,
obj_udfchar15 as OBJ_UDFCHAR15,
obj_udfchar16 as OBJ_UDFCHAR16,
obj_udfchar17 as OBJ_UDFCHAR17,
obj_udfchar18 as OBJ_UDFCHAR18,
obj_udfchar19 as OBJ_UDFCHAR19,
obj_udfchar20 as OBJ_UDFCHAR20,
obj_udfchar21 as OBJ_UDFCHAR21,
obj_udfchar22 as OBJ_UDFCHAR22,
obj_udfchar23 as OBJ_UDFCHAR23,
obj_udfchar24 as OBJ_UDFCHAR24,
obj_udfchar25 as OBJ_UDFCHAR25,
obj_udfchar26 as OBJ_UDFCHAR26,
obj_udfchar27 as OBJ_UDFCHAR27,
obj_udfchar28 as OBJ_UDFCHAR28,
obj_udfchar29 as OBJ_UDFCHAR29,
obj_udfchar30 as OBJ_UDFCHAR30,
obj_udfnum01  as OBJ_UDFNUM01,
obj_udfnum02  as OBJ_UDFNUM02,
obj_udfnum03  as OBJ_UDFNUM03,
obj_udfnum04  as OBJ_UDFNUM04,
obj_udfnum05  as OBJ_UDFNUM05,
obj_udfdate01 as OBJ_UDFDATE01,
obj_udfdate02 as OBJ_UDFDATE02,
obj_udfdate03 as OBJ_UDFDATE03,
obj_udfdate04 as OBJ_UDFDATE04,
obj_udfdate05 as OBJ_UDFDATE05,
obj_udfchkbox01 as OBJ_UDFCHKBOX01,
obj_udfchkbox02 as OBJ_UDFCHKBOX02,
obj_udfchkbox03 as OBJ_UDFCHKBOX03,
obj_udfchkbox04 as OBJ_UDFCHKBOX04,
obj_udfchkbox05 as OBJ_UDFCHKBOX05
FROM  R5OBJECTS o 
WHERE      
obj_rstatus not in ('A', 'D')
AND obj_obrtype in ('A','S','P','L')      
AND obj_notused = '-'
AND
((<parameter.deptsec>  = 'OFF' ) 
 OR
EXISTS( 
        SELECT 1
        FROM r5departmentsecurity
        WHERE dse_user = <parameter.r5user> 
        AND
        dse_mrc = obj_mrc 
       ) 
)
ORDER BY DMLTYPE,
<parameter.fieldname> <parameter.sortorder>
