"For Loop" Oracle PL/SQL 中的迭代计数
Iteration Count in "For Loop" Oracle PL/SQL
写PLSQL块显示给定数字中的奇数和总奇数?例:如果给定的数是3,则显示如下 1是奇数 3是奇数 Total 2 odd numbers in 3
CREATE OR REPLACE PROCEDURE odd_num(p_num NUMBER )
IS s_num NUMBER;
BEGIN
FOR i_num IN 1..p_num
LOOP
IF mod(i_num,2) = 1
THEN
dbms_output.put_line(i_num ||' is Odd Number');
END IF;
END LOOP;
dbms_output.put_line('Total '|| s_num ||' Odd Numbers in '||p_num);
END;
您只需初始化 计数器 并将其分配给 s_num
。只需在 IF 块中添加以下内容:
s_num := s_num +1;
例如,
SQL> CREATE OR REPLACE PROCEDURE odd_num(p_num NUMBER )
2 IS
3 s_num NUMBER;
4 BEGIN
5 s_num :=0;
6 FOR i_num IN 1..p_num
7 LOOP
8 IF mod(i_num,2) = 1
9 THEN
10 -- Increment the counter once for each iteration
11 s_num := s_num +1;
12 dbms_output.put_line(i_num ||' is Odd Number');
13 END IF;
14 END LOOP;
15 dbms_output.put_line('Total '|| s_num ||' Odd Numbers in '||p_num);
16 END;
17 /
Procedure created.
SQL> sho err
No errors.
让我们执行 过程:
SQL> SET SERVEROUTPUT ON
SQL> EXEC odd_num(5);
1 is Odd Number
3 is Odd Number
5 is Odd Number
Total 3 Odd Numbers in 5
PL/SQL procedure successfully completed.
SQL>
写PLSQL块显示给定数字中的奇数和总奇数?例:如果给定的数是3,则显示如下 1是奇数 3是奇数 Total 2 odd numbers in 3
CREATE OR REPLACE PROCEDURE odd_num(p_num NUMBER )
IS s_num NUMBER;
BEGIN
FOR i_num IN 1..p_num
LOOP
IF mod(i_num,2) = 1
THEN
dbms_output.put_line(i_num ||' is Odd Number');
END IF;
END LOOP;
dbms_output.put_line('Total '|| s_num ||' Odd Numbers in '||p_num);
END;
您只需初始化 计数器 并将其分配给 s_num
。只需在 IF 块中添加以下内容:
s_num := s_num +1;
例如,
SQL> CREATE OR REPLACE PROCEDURE odd_num(p_num NUMBER )
2 IS
3 s_num NUMBER;
4 BEGIN
5 s_num :=0;
6 FOR i_num IN 1..p_num
7 LOOP
8 IF mod(i_num,2) = 1
9 THEN
10 -- Increment the counter once for each iteration
11 s_num := s_num +1;
12 dbms_output.put_line(i_num ||' is Odd Number');
13 END IF;
14 END LOOP;
15 dbms_output.put_line('Total '|| s_num ||' Odd Numbers in '||p_num);
16 END;
17 /
Procedure created.
SQL> sho err
No errors.
让我们执行 过程:
SQL> SET SERVEROUTPUT ON
SQL> EXEC odd_num(5);
1 is Odd Number
3 is Odd Number
5 is Odd Number
Total 3 Odd Numbers in 5
PL/SQL procedure successfully completed.
SQL>