create or replace PROCEDURE queueProcessing3 (starterName IN VARCHAR2, selectDepth IN SMALLINT, rowsToSend out pkg_queueprocessing.refCurOut) AS BEGIN FOR currentRow IN ( select * from message_queue where entryid in (select entryid from ( SELECT ENTRYID, PRIORITY, ENQUEUE_TIMESTAMP, number_of_req, conf_FREE_SLOTS, taken_FREE_SLOTS FROM ( select ENTRYID, PRIORITY, ENQUEUE_TIMESTAMP, number_of_req, conf_FREE_SLOTS, (select count(*) from message_queue where STATUS = 'WORK' and RECEIVER_ID = receiverId) taken_FREE_SLOTS from (select ENTRYID, PRIORITY, RECEIVER_ID receiverId, CORRELATION_ID, ENQUEUE_TIMESTAMP, ROW_NUMBER() OVER (PARTITION BY RECEIVER_ID order by PRIORITY desc, ENQUEUE_TIMESTAMP) number_of_req, KEY conf_key, nvl(value,(select value from message_queue_config where trim(UPPER(key)) = 'FREE_SLOTS_DEFAULT' and ROWNUM <= 1)) conf_FREE_SLOTS from message_queue, message_queue_config where status = 'WAIT' and 'FREE_SLOTS_' || trim(UPPER(receiver_id)) = trim(UPPER(key(+))) order by PRIORITY desc, ENQUEUE_TIMESTAMP )) WHERE number_of_req <= (conf_FREE_SLOTS - taken_FREE_SLOTS) AND ROWNUM <= selectDepth) ) for update nowait) LOOP UPDATE message_queue set status = 'TOSEND' WHERE entryid = currentRow.entryid; END LOOP; OPEN rowsToSend FOR select * from message_queue where status = 'TOSEND' order by priority desc, enqueue_timestamp; update message_queue set status = 'WORK', status_message = 'Status TOSEND updated to WORK by the procedure QUEUEPROCESSING initiated by ' || starterName, status_timestamp = current_timestamp, status_work_timestamp = current_timestamp where status = 'TOSEND'; exception when others then ROLLBACK; END queueProcessing3;