select
tsk_jobplan as job_selector,
tpj_seq as tpj_seq,
TSK_CODE as tsk_code,
TSK_DESC as tsk_desc
FROM (
SELECT 0 as tpj_seq, 'SHOWALLRECORDS' as tsk_code, <param.showAllJobs> as tsk_desc, <param.showAllJobs> as tsk_jobplan
FROM dual
UNION ALL
SELECT tpj_seq, tsk_code, tsk_desc, tpj_seq || '-' || tsk_desc as tsk_jobplan
FROM r5operatorchecklists,r5taskplanjobs,r5tasks
WHERE tpj_task = ock_task
AND tsk_code=TPJ_JOBPLAN
AND ock_code = <param.operatorchecklistnumber>
AND EXISTS (SELECT 1 FROM r5actchecklists
WHERE ACK_FROMJOBPLAN=tsk_code AND ack_fromjobplanrev=tsk_revision
AND ock_code=ack_entitykey AND ock_org=ack_entityorg AND ack_rentity = 'OPCK')
) A
WHERE 1=1
