Dear community
We use SonarQube version Version 6.7.6
The following code is flagged as a Bug (Nested loops should be labeled):
l_start_loop := l_start_date;
WHILE l_start_loop <= l_end_date LOOP
<<period_data>>
OPEN c_in_invoice_report(l_start_loop);
k := 1;
WHILE k > 0 LOOP
<<nestedLoop>>
FETCH c_in_invoice_report BULK COLLECT INTO l_in_invoice_report LIMIT g_limit_long;
FORALL i IN 1 .. l_in_invoice_report.count SAVE EXCEPTIONS INSERT INTO in_invoice_report VALUES l_in_invoice_report (i);
k := l_in_invoice_report.count;
END LOOP nestedLoop;
l_start_loop := add_months(l_start_loop, 1);
CLOSE c_in_invoice_report;
COMMIT;
END LOOP period_data;
The code works fine, and I can’t find anything wrong in it.
What can I do to cleanup the code, and make it compliant?
Thanks for your help
In PL/SQL, the “label name” should be added before the statement that should be labeled. When SonarQube says that “nested loops should be labeled” it means it’s expecting:
<<label_name>>
WHILE ...
Note the label before the WHILE statement. In your example, the code is labeling the OPEN statement as period_data and FETCH statement as nestedLoop.
Considering this, the corrected code is:
l_start_loop := l_start_date;
<<period_data>>
WHILE l_start_loop <= l_end_date LOOP
OPEN c_in_invoice_report(l_start_loop);
k := 1;
<<nestedLoop>>
WHILE k > 0 LOOP
FETCH c_in_invoice_report BULK COLLECT INTO l_in_invoice_report LIMIT g_limit_long;
FORALL i IN 1 .. l_in_invoice_report.count SAVE EXCEPTIONS INSERT INTO in_invoice_report VALUES l_in_invoice_report (i);
k := l_in_invoice_report.count;
END LOOP nestedLoop;
l_start_loop := add_months(l_start_loop, 1);
CLOSE c_in_invoice_report;
COMMIT;
END LOOP period_data;