File tree 2 files changed +59
-0
lines changed
4-language-usage/4-control-structures/3-flow-control
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ # G-4387: Never use a FOR LOOP for a query that should return not more than one row.
2
+
3
+ !!! bug "Blocker"
4
+ Reliability, Efficiency, Readability
5
+
6
+ !!! missing "Unsupported in db\* CODECOP Validators"
7
+ Without access to the Oracle Data Dictionary, we cannot determine the number of rows to be processed.
8
+
9
+ ## Reason
10
+
11
+ A ` for loop ` can hide a ` too_many_rows ` exception. The more complex a query is, the higher is the risk that more than one row will be processed.
12
+ This affects performance and can lead to a wrong result.
13
+
14
+ A ` for loop ` can also hide a ` no_data_found ` exception and the reader cannot determine whether this is intentional or not.
15
+
16
+ ## Example (bad)
17
+
18
+ ``` sql
19
+ create or replace package body employee_api is
20
+ function emp_name(in_empno in integer ) return varchar2 is -- NOSONAR: non-deterministic
21
+ l_ename emp .ename %type;
22
+ begin
23
+ << fetch_name>>
24
+ for r in (
25
+ select ename
26
+ from emp
27
+ where empno = in_empno
28
+ )
29
+ loop
30
+ l_ename := r .ename ;
31
+ end loop fetch_name;
32
+ return l_ename;
33
+ end emp_name;
34
+ end employee_api;
35
+ /
36
+ ```
37
+
38
+ ## Example (good)
39
+
40
+ ``` sql
41
+ create or replace package body employee_api is
42
+ function emp_name(in_empno in integer ) return varchar2 is -- NOSONAR: non-deterministic
43
+ l_ename emp .ename %type;
44
+ begin
45
+ select ename
46
+ into l_ename
47
+ from emp
48
+ where empno >= in_empno;
49
+ return l_ename;
50
+ exception
51
+ when no_data_found then
52
+ return null ;
53
+ when too_many_rows then
54
+ raise;
55
+ end emp_name;
56
+ end employee_api;
57
+ /
58
+ ```
Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ n/a | 4365 | Never use unconditional CONTINUE or EXIT in a loop. | Major | | |
87
87
46 | 4375 | Always use EXIT WHEN instead of an IF statement to exit from a loop. | Minor | | | ✘ ; | | | | |
88
88
47 | 4380 | Try to label your EXIT WHEN statements. | Minor | | | ✘ ; | | | | |
89
89
48 | 4385 | Never use a cursor for loop to check whether a cursor returns data. | Critical | | ✘ ; | | | | | |
90
+ n/a | 4387 | !!!CHARACTERISTIC ERROR!!! | Blocker | | ✘ ; | | | ✘ ; | | |
90
91
49 | 4390 | Avoid use of unreferenced FOR loop indexes. | Major | | ✘ ; | | | | | |
91
92
50 | 4395 | Avoid hard-coded upper or lower bound values with FOR loops. | Minor | ✘ ; | | ✘ ; | | | | |
92
93
n/a | 5010 | Try to use a error/logging framework for your application. | Critical | | | | | ✘ ; | ✘ ; | | ✘ ;
You can’t perform that action at this time.
0 commit comments