oracleserver中的sql嵌套循环

wfauudbj  于 2021-08-01  发布在  Java
关注(0)|答案(2)|浏览(333)

我不是软件程序员。我想为我的工作学习pl/sql。我检查了一个匿名过程,包括嵌套的基本循环,如下所示。由于这个匿名块被发现“166”。我无法理解如何在下面的语句中迭代for循环。有人能帮我解决这个问题吗?如果你能帮助我解决这个问题,我将不胜感激。

DECLARE
s PLS_INTEGER := 0;
i PLS_INTEGER := 0;
j PLS_INTEGER;
BEGIN
<<outer_loop>>
LOOP
i := i + 1;
j := 0;
<<inner_loop>>
LOOP
j := j + 1;
s := s + i * j; -- Sum several products
EXIT inner_loop WHEN (j > 5);
EXIT outer_loop WHEN ((i * j) > 15);
END LOOP inner_loop;
END LOOP outer_loop;
DBMS_OUTPUT.PUT_LINE
('The sum of products equals: ' || TO_CHAR(s));
END;
/

已处理对账单。积和等于:166

hts6caw3

hts6caw31#

为了更好地理解循环是如何结束的,您需要在流程中添加一些行来帮助您理解为什么会出现循环

SQL> SET SERVEROUTPUT ON SIZE UNLIMITED
 SQL> DECLARE
  2  s PLS_INTEGER := 0;
  3  i PLS_INTEGER := 0;
  4  j PLS_INTEGER;
  5  BEGIN
  6  <<outer_loop>>
  7  LOOP
  8  i := i + 1;
  9  j := 0;
 10  <<inner_loop>>
 11  LOOP
 12  j := j + 1;
 13  s := s + i * j; -- Sum several products
 14  dbms_output.put_line( ' I is : '||to_char(i)||' ');
 15  dbms_output.put_line( ' J is : '||to_char(j)||' ');
 16  dbms_output.put_line( ' S is : '||to_char(s)||' ');
 17  EXIT inner_loop WHEN (j > 5);
 18  EXIT outer_loop WHEN ((i * j) > 15);
 19  END LOOP inner_loop;
 20  END LOOP outer_loop;
 21  DBMS_OUTPUT.PUT_LINE
 22  ('The sum of products equals: ' || TO_CHAR(s));
 23* END;
SQL> /
I is : 1
J is : 1
S is : 1
I is : 1
J is : 2
S is : 3
I is : 1
J is : 3
S is : 6
I is : 1
J is : 4
S is : 10
I is : 1
J is : 5
S is : 15
I is : 1
J is : 6
S is : 21
I is : 2
J is : 1
S is : 23
I is : 2
J is : 2
S is : 27
I is : 2
J is : 3
S is : 33
I is : 2
J is : 4
S is : 41
I is : 2
J is : 5
S is : 51
I is : 2
J is : 6
S is : 63
I is : 3
J is : 1
S is : 66
I is : 3
J is : 2
S is : 72
I is : 3
J is : 3
S is : 81
I is : 3
J is : 4
S is : 93
I is : 3
J is : 5
S is : 108
I is : 3
J is : 6
S is : 126
I is : 4
J is : 1
S is : 130
I is : 4
J is : 2
S is : 138
I is : 4
J is : 3
S is : 150
I is : 4
J is : 4
S is : 166
The sum of products equals: 166

PL/SQL procedure successfully completed.

SQL>

循环应用您在代码中得到的公式,它是应用于两个循环的出口,定义了循环何时结束。在您的例子中,第一个循环应该在j>5时结束,但是您有一个需要在之前结束的二次循环,它只在((i*j)>15时结束,这只在i=4和j=4时发生,所以16。
希望能澄清一点

k5hmc34c

k5hmc34c2#

您只需在内部\u循环中使用一个dbms \u输出即可获得此想法,如下所示:

SQL> SET SERVEROUT ON
SQL>
SQL> DECLARE
  2  S   PLS_INTEGER := 0;
  3  I   PLS_INTEGER := 0;
  4  J   PLS_INTEGER;
  5  BEGIN
  6  <<OUTER_LOOP>> LOOP
  7  I   := I + 1;
  8  J   := 0;
  9  <<INNER_LOOP>> LOOP
 10  J   := J + 1;
 11  S   := S + I * J; -- Sum several products
 12  DBMS_OUTPUT.PUT_LINE('I: ' || I || ' , J: ' || J || ' , S: ' || S);
 13  EXIT INNER_LOOP WHEN ( J > 5 );
 14  EXIT OUTER_LOOP WHEN ( ( I * J ) > 15 );
 15  END LOOP INNER_LOOP;
 16  END LOOP OUTER_LOOP;
 17  DBMS_OUTPUT.PUT_LINE('The sum of products equals: ' || TO_CHAR(S));
 18  END;
 19  /
I: 1 , J: 1 , S: 1
I: 1 , J: 2 , S: 3
I: 1 , J: 3 , S: 6
I: 1 , J: 4 , S: 10
I: 1 , J: 5 , S: 15
I: 1 , J: 6 , S: 21
I: 2 , J: 1 , S: 23
I: 2 , J: 2 , S: 27
I: 2 , J: 3 , S: 33
I: 2 , J: 4 , S: 41
I: 2 , J: 5 , S: 51
I: 2 , J: 6 , S: 63
I: 3 , J: 1 , S: 66
I: 3 , J: 2 , S: 72
I: 3 , J: 3 , S: 81
I: 3 , J: 4 , S: 93
I: 3 , J: 5 , S: 108
I: 3 , J: 6 , S: 126
I: 4 , J: 1 , S: 130
I: 4 , J: 2 , S: 138
I: 4 , J: 3 , S: 150
I: 4 , J: 4 , S: 166
The sum of products equals: 166

PL/SQL procedure successfully completed.

SQL>

相关问题