oracle 使用for循环如何增加任何数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4292987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 22:03:20  来源:igfitidea点击:

using for loop how to increment any number

sqloracleplsql

提问by user636464

Write a program containing a loop that iterates from 1 to 1000 using a variable I, which is incremented each time around the loop. The program should output the value of Ievery hundred iterations (i.e., the output should be 100, 200, etc). Display the output on the screen using dbms_output.put_line.

编写一个包含循环的程序,该循环使用一个变量从 1 到 1000 进行迭代,该变量I每次围绕循环递增。程序应该输出I每一百次迭代的值(即输出应该是 100、200 等)。使用 将输出显示在屏幕上dbms_output.put_line

回答by OMG Ponies

Use:

用:

FOR I IN 1..1000
LOOP
   IF MOD(I, 100) = 0 THEN
     DBMS_OUTPUT.PUT_LINE(I);
   END IF;
END LOOP;

Reference:

参考:

回答by Jeffrey Kemp

This might be faster:

这可能更快:

begin
 dbms_output.put_line('100');
 dbms_output.put_line('200');
 dbms_output.put_line('300');
 dbms_output.put_line('400');
 dbms_output.put_line('500');
 dbms_output.put_line('600');
 dbms_output.put_line('700');
 dbms_output.put_line('800');
 dbms_output.put_line('900');
 dbms_output.put_line('1000');
end;

:)

:)

回答by Bob Jarvis - Reinstate Monica

Or you could implement this as

或者您可以将其实现为

FOR I IN 1..1000 LOOP
  IF I IN (100, 200, 300, 400, 500, 600, 700, 800, 900, 1000) THEN
    DBMS_OUTPUT.PUT_LINE(I);
  END IF;
END LOOP;

or

或者

FOR I IN 1..1000 LOOP
  IF I/100 = TRUNC(I/100) THEN
    DBMS_OUTPUT.PUT_LINE(I);
  END IF;
END LOOP;

or even

甚至

DECLARE
  INPUT_NUM   NUMBER;
  OUTPUT_NUM  NUMBER;
BEGIN
  FOR I IN 1..1000 LOOP
    SELECT I/100, TRUNC(I/100)
      INTO INPUT_NUM, OUTPUT_NUM
      FROM DUAL;

    IF INPUT_NUM = OUTPUT_NUM THEN
      DBMS_OUTPUT.PUT_LINE(I);
    END IF;
  END LOOP;
END;

Share and enjoy.

分享和享受。