Oracle Forms 10g多行复制至另一块代码死循环问题求助
Hey there! Let's dig into why your Oracle Forms code is stuck in an infinite loop and fix it up for you.
What's Causing the Loop?
Your original code has a few key issues that lead to the endless cycle:
- Syntax Error: The line
l_job := :first.jobis missing a trailing semicolon. This breaks compilation and can trigger unexpected runtime behavior, including loops that don't terminate. - Unreliable Record Traversal: Using an incrementing counter (
l_currec) to jump between records is risky. When the counter exceeds the actual number of records in the first block, Oracle Forms won't jump to a non-existent record—it'll just stay on the last one. This means your code will keep copying the final record to the second block forever. - Timing of Exit Logic: The exit check happens after copying the record, but the counter keeps increasing even when you're already on the last record, leading to repeated, unnecessary iterations.
Fixed Code
Here's a revised version that safely traverses records and avoids the loop:
DECLARE -- Declare variables matching the fields you want to copy l_empno emp.empno%TYPE; l_ename emp.ename%TYPE; l_job emp.job%TYPE; BEGIN -- Navigate to the source block and start at the first record GO_BLOCK('first'); FIRST_RECORD; LOOP -- Save values from the current source record l_empno := :first.empno; l_ename := :first.ename; l_job := :first.job; -- Fixed the missing semicolon here -- Move to the target block, go to the end, and create a new record GO_BLOCK('second'); LAST_RECORD; CREATE_RECORD; -- Populate the new target record with saved values :second.empno := l_empno; :second.ename := l_ename; :second.job := l_job; -- Exit the loop if we just processed the last source record EXIT WHEN :system.last_record = 'TRUE'; -- Move to the next record in the source block GO_BLOCK('first'); NEXT_RECORD; END LOOP; END;
Key Improvements
- Safe Record Traversal: Using
FIRST_RECORDandNEXT_RECORDensures we only move through existing records in the source block—no more stuck-on-last-record issues. - Fixed Syntax: Added the missing semicolon to ensure the code compiles and runs as expected.
- Clean Exit Logic: We check if we're on the last source record right after copying, so the loop terminates immediately after processing the final row.
Quick Extra Tips
- If your source block might be empty, add a check upfront:
IF :system.record_count = 0 THEN RETURN; END IF;to skip the loop entirely. - If you need to clear existing records in the target block before copying, add
CLEAR_BLOCK(NO_VALIDATE);right afterGO_BLOCK('second');(adjust the validation flag based on your needs).
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

