SELECT uco_code Status, uco_desc as Description FROM r5ucodes WHERE uco_rentity = <param.ucorentitycode> AND (uco_code IN (SELECT aut_statnew FROM r5auth WHERE aut_rentity = <param.entitycode> AND aut_type='+' AND (aut_status = <param.statuscode> OR aut_status = '*') AND ( aut_user = <parameter.r5user> OR (aut_group = <param.groupcode> AND aut_user = '*'))) AND uco_code not in (SELECT aut_statnew FROM r5auth WHERE aut_rentity = <param.entitycode> AND aut_type='-' AND (aut_status = <param.statuscode>  OR aut_status = '*') AND (aut_user = <parameter.r5user> OR (aut_group = <param.groupcode> and aut_user='*'))) OR uco_code = <param.statuscode>) 
AND (CASE EXISTS (SELECT 1 FROM r5ucodes u1 WHERE u1.uco_rentity=<param.ucorentitycode> AND u1.uco_code=<param.statuscode> AND u1.uco_rcode IN ('U','RA','A')) WHEN 1 THEN uco_rcode NOT IN ('EXP','SUSP') ELSE 1=1 END)
AND (CASE EXISTS (SELECT 1 FROM r5ucodes u1 WHERE u1.uco_rentity=<param.ucorentitycode> AND u1.uco_code=<param.statuscode> AND u1.uco_rcode IN ('ACT','SUSP')) WHEN 1 THEN uco_rcode <> 'EXP' ELSE 1=1 END)
AND (CASE EXISTS (SELECT 1 FROM r5ucodes u1 WHERE u1.uco_rentity=<param.ucorentitycode> AND u1.uco_code = <param.statuscode> AND u1.uco_rcode = 'C') WHEN 1 THEN uco_rcode='C' ELSE 1=1 END)
AND (CASE EXISTS (SELECT 1 FROM r5ucodes u1 WHERE u1.uco_rentity = <param.ucorentitycode> AND u1.uco_code=<param.statuscode> AND u1.uco_rcode ='CAN') WHEN 1 THEN uco_rcode='CAN' ELSE 1=1 END)
