相关文章推荐
乐观的哑铃  ·  SQL Server 2012 ...·  2 年前    · 
玩篮球的手电筒  ·  即使有连接 ...·  2 年前    · 
高大的丝瓜  ·  ExoPlayer和启动/暂停/ ...·  2 年前    · 

GOTO’s should not be part of any modern programming language (IMHO). They are bad for so many reasons and should be avoided at all costs. I guess they were carried over from the Assembly and later the Basic language days. Good code (procedure/function/block) should have a single entry point and a single exit point to make it readable and maintainable. I often see GOTO’s in code database developers put out and I cringe!

In any case, if you have GOTO’s in your PL/SQL code and need to replace them with LOOP’s which are more readable, I show how to do this below with a simple “BEFORE” and “AFTER” example with relevant replacements highlighted.

BEFORE – Using GOTO

FOR vcAccounting IN cAccounting BEGIN IF (NOT SOMETHING) GOTO L_ACTUALS; END IF IF (SOMETHINGELSE) --PROCESS... GOTO L_ACTUALS; END IF BEGIN --MORE PROCESSING << L_ACTUALS >> NULL; EXCEPTION WHEN xNonAcct_Err THEN IF vStatRecWritten THEN END IF; END LOOP; -- vcAccounting IN cAccounting

AFTER – Using LOOP

FOR vcAccounting IN cAccounting BEGIN WHILE TRUE LOOP IF (NOT SOMETHING) EXIT; END IF IF (SOMETHINGELSE) --PROCESS... EXIT; END IF BEGIN --MORE PROCESSING EXIT; END LOOP; EXCEPTION WHEN xNonAcct_Err THEN IF vStatRecWritten THEN END IF; END LOOP; -- vcAccounting IN cAccounting

You see how simple that was? Now, you don’t have an excuse to use GOTO’s in PL/SQL (or anywhere else)!