Download - PLSQL Coding Guidelines - Part 6
![Page 1: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/1.jpg)
PL/SQL & SQL CODING GUIDELINES – PART 6Larry Nung
![Page 2: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/2.jpg)
AGENDAControl Structures
Flow ControlReferenceQ&A
2
![Page 3: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/3.jpg)
CONTROL STRUCTURESFlow Control
![Page 4: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/4.jpg)
39. NEVER USE GOTO STATEMENTS IN YOUR CODE.
![Page 5: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/5.jpg)
40. ALWAYS LABEL YOUR LOOPS.
![Page 6: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/6.jpg)
GOODBEGIN <<process_employees>> FOR r_employee IN (SELECT * FROM emp) LOOP … END LOOP process_employees; END;
![Page 7: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/7.jpg)
41. ALWAYS USE A CURSOR FOR LOOP TO PROCESS THE COMPLETE CURSOR RESULTS UNLESS YOU ARE USING BULK OPERATIONS.
![Page 8: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/8.jpg)
GOODBEGIN <<read_employees>> FOR r_employee IN c_employee LOOP … END LOOP read_employees; END;
![Page 9: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/9.jpg)
42. ALWAYS USE A NUMERIC FOR LOOP TO PROCESS A DENSE ARRAY.
![Page 10: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/10.jpg)
GOODBEGIN <<process_employees>> FOR i IN 1..t_employees.COUNT() LOOP … END LOOP process_employees; END;
![Page 11: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/11.jpg)
43. USE 1 AS LOWER BOUNDARY AND COUNT() AS UPPER BOUNDARY WHEN LOOPING OVER ADENSE ARRAY.
![Page 12: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/12.jpg)
BADDECLARE t_employees t_employee_type :=
t_employee_type(); BEGIN <<process_employees>> FOR i IN
t_employees.FIRST()..t_employees.LAST() LOOP … END LOOP process_employees; END;
![Page 13: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/13.jpg)
GOODDECLARE t_employees t_employee_type := t_employee_type(); BEGIN <<process_employees>> IF t_employees IS NOT EMPTY THEN FOR i IN t_employees.FIRST()..t_employees.LAST() LOOP … END LOOP process_employees; END IF; END;
![Page 14: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/14.jpg)
GOODDECLARE t_employees t_employee_type :=
t_employee_type(); BEGIN <<process_employees>> FOR i IN 1..t_employees.COUNT() LOOP … END LOOP process_employees; END;
![Page 15: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/15.jpg)
44. ALWAYS USE A WHILE LOOP TO PROCESS A LOOSE ARRAY.
![Page 16: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/16.jpg)
GOODDECLARE l_index PLS_INTEGER; BEGIN l_index := t_employees.FIRST();
<<process_employees>> WHILE l_index IS NOT NULL LOOP … l_index := t_employees.NEXT(l_index); END LOOP process_employees; END;
![Page 17: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/17.jpg)
45. AVOID USING EXIT TO STOP LOOP PROCESSING UNLESS YOU ARE IN A BASIC LOOP.
![Page 18: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/18.jpg)
46. ALWAYS USE EXIT WHEN INSTEAD OF AN IF STATEMENT TO EXIT FROM A LOOP.
![Page 19: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/19.jpg)
BADBEGIN <<process_employees>> LOOP ... IF ... THEN EXIT process_employees; END IF; ... END LOOP process_employees; END;
![Page 20: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/20.jpg)
GOODBEGIN <<process_employees>> LOOP ... EXIT process_employees WHEN (...); END LOOP process_employees; END;
![Page 21: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/21.jpg)
47. TRY TO LABEL YOUR EXIT WHEN STATEMENTS.
![Page 22: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/22.jpg)
GOODBEGIN l_outerlp := 0;
<<outerloop>> LOOP l_innerlp := 0; l_outerlp := NVL(l_outerlp,0) + 1;
<<innerloop>> LOOP l_innerlp := NVL(l_innerlp,0) + 1; DBMS_OUTPUT.PUT_LINE('Outer Loop counter is ' || l_outerlp || ' Inner
Loop counter is ' || l_innerlp); EXIT outerloop WHEN l_innerlp = 3; END LOOP innerloop; END LOOP outerloop; END;
![Page 23: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/23.jpg)
48. DO NOT USE A CURSOR FOR LOOP TO CHECK WHETHER A CURSOR RETURNS DATA.
![Page 24: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/24.jpg)
BADDECLARE l_employee_found BOOLEAN := FALSE; … BEGIN <<check_employees>> FOR r_employee IN c_employee LOOP l_employee_found := TRUE; END LOOP check_employees; END;
![Page 25: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/25.jpg)
GOODDECLARE l_employee_found BOOLEAN := FALSE; … BEGIN OPEN c_employee; FETCH c_employee INTO r_employee; l_employee_found := c_employee%FOUND; CLOSE c_emplyoee; END;
![Page 26: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/26.jpg)
49. AVOID USE OF UNREFERENCED FOR LOOP INDEXES.
![Page 27: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/27.jpg)
50. AVOID HARD-CODED UPPER OR LOWER BOUND VALUES WITH FOR LOOPS.
![Page 28: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/28.jpg)
REFERENCE28
![Page 29: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/29.jpg)
REFERENCE Trivadis PL/SQL & SQL Coding Guidelines
Version 2.0 http://www.trivadis.com/sites/default/files/downlo
ads/PLSQL_and_SQL_Coding_Guidelines_2_0_HiRes.pdf
29
![Page 30: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/30.jpg)
Q&A30
![Page 31: PLSQL Coding Guidelines - Part 6](https://reader033.vdocuments.net/reader033/viewer/2022051318/58b88cc11a28ab3e3a8b4e6f/html5/thumbnails/31.jpg)
QUESTION & ANSWER
31