SELECT
NCF_CODE as nonconformity,
NCF_DESC as description,
NCF_ORG as organization,
NCF_OBJECT as equipment,
OBJ_DESC as equipmentdesc,
NCF_OBJECT_ORG as equipmentorg,
NCF_NCTTYPE AS type,
(
SELECT NCT_DESC FROM r5nonconformitytypes WHERE NCT_CODE = NCF_NCTTYPE
)
AS typedesc,
NCF_STATUS AS status,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'NCST' AND UCO_CODE = NCF_STATUS
)
AS headerstatus,
(
SELECT UCO_RCODE FROM r5ucodes WHERE uco_rentity = 'NCST' AND UCO_CODE = NCF_STATUS
)
AS rstatus,
NCF_SEVERITY AS severity,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'SEVE' AND UCO_CODE = NCF_SEVERITY
) AS headerseverity,
NCF_NCTTYPE as type
FROM r5nonconformities
LEFT OUTER JOIN r5objects ON ( obj_code = ncf_object AND obj_org = ncf_object_org ) 
WHERE ncf_object = <param.object>
AND ncf_object_org = <param.objectorg>
AND (<parameter.deptsec> = 'OFF' OR
(EXISTS( SELECT dse_mrc FROM r5departmentsecurity
WHERE dse_user = <parameter.r5user> AND dse_mrc = obj_mrc)))

