Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Oracle LOOP loop control statement

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In PL/SQL, the loop statement can be used to loop the data, which can be used to loop through the specified sequence of statements. Commonly used LOOP loop statements come in three forms: basic LOOP, WHILE...LOOP, and FOR...LOOP. The basic syntax structure of the LOOP statement is as follows: []

LOOP

Statement...

END LOOP [label_name]

[syntax description]

The label of the LOOP structure, which is optional

LOOP:LOOP loop start flag.

The sequence of statements executed in a loop in a statement:LOOP statement.

The end of the END LOOP:LOOP loop flag, you can add tags for the LOOP structure.

1. Basic LOOP statement

Example: a variable is required to be declared, and each loop needs to add the number 1 to the variable and output the result. Exit the loop operation when the variable value is greater than 100.

A. Use EXIT...WHEN to end the loop operation.

SQL > declare

2 v_rlt number (3): = 1

3 begin

four

5 loop

6 dbms_output.put_line ('v_rlt =' | | v_rlt)

7 v_rlt:=v_rlt+1

8 exit fst_loop when v_rlt > 100

9 end loop

10 dbms_output.put_line ('LOOP cycle is over!')

11 end

12 /

V_rlt = 1

V_rlt = 2

V_rlt = 3

V_rlt = 4

V_rlt = 5

V_rlt = 6

V_rlt = 7

V_rlt = 8

V_rlt = 9

V_rlt = 10

V_rlt = 11

V_rlt = 12

V_rlt = 13

V_rlt = 14

V_rlt = 15

V_rlt = 16

V_rlt = 17

V_rlt = 18

V_rlt = 19

V_rlt = 20

V_rlt = 21

V_rlt = 22

V_rlt = 23

V_rlt = 24

V_rlt = 25

V_rlt = 26

V_rlt = 27

V_rlt = 28

V_rlt = 29

V_rlt = 30

V_rlt = 31

V_rlt = 32

V_rlt = 33

V_rlt = 34

V_rlt = 35

V_rlt = 36

V_rlt = 37

V_rlt = 38

V_rlt = 39

V_rlt = 40

V_rlt = 41

V_rlt = 42

V_rlt = 43

V_rlt = 44

V_rlt = 45

V_rlt = 46

V_rlt = 47

V_rlt = 48

V_rlt = 49

V_rlt = 50

V_rlt = 51

V_rlt = 52

V_rlt = 53

V_rlt = 54

V_rlt = 55

V_rlt = 56

V_rlt = 57

V_rlt = 58

V_rlt = 59

V_rlt = 60

V_rlt = 61

V_rlt = 62

V_rlt = 63

V_rlt = 64

V_rlt = 65

V_rlt = 66

V_rlt = 67

V_rlt = 68

V_rlt = 69

V_rlt = 70

V_rlt = 71

V_rlt = 72

V_rlt = 73

V_rlt = 74

V_rlt = 75

V_rlt = 76

V_rlt = 77

V_rlt = 78

V_rlt = 79

V_rlt = 80

V_rlt = 81

V_rlt = 82

V_rlt = 83

V_rlt = 84

V_rlt = 85

V_rlt = 86

V_rlt = 87

V_rlt = 88

V_rlt = 89

V_rlt = 90

V_rlt = 91

V_rlt = 92

V_rlt = 93

V_rlt = 94

V_rlt = 95

V_rlt = 96

V_rlt = 97

V_rlt = 98

V_rlt = 99

V_rlt = 100

The LOOP cycle is over!

PL/SQL procedure successfully completed.

B. End the loop by using if.. exit statement

SQL > declare

2 v_rlt number (3): = 1

3 begin

four

5 loop

6 dbms_output.put_line ('v_rlt =' | | v_rlt)

7 v_rlt:=v_rlt+1

8 if v_rlt > 100 then

9 dbms_output.put_line (the value of the 'variable is greater than 100, and the current value is' | | v_rlt)

10 exit fst_loop

11 end if

12 end loop

13 dbms_output.put_line ('LOOP cycle is over!')

14 end

15 /

V_rlt = 1

V_rlt = 2

V_rlt = 3

V_rlt = 4

V_rlt = 5

V_rlt = 6

V_rlt = 7

V_rlt = 8

V_rlt = 9

V_rlt = 10

V_rlt = 11

V_rlt = 12

V_rlt = 13

V_rlt = 14

V_rlt = 15

V_rlt = 16

V_rlt = 17

V_rlt = 18

V_rlt = 19

V_rlt = 20

V_rlt = 21

V_rlt = 22

V_rlt = 23

V_rlt = 24

V_rlt = 25

V_rlt = 26

V_rlt = 27

V_rlt = 28

V_rlt = 29

V_rlt = 30

V_rlt = 31

V_rlt = 32

V_rlt = 33

V_rlt = 34

V_rlt = 35

V_rlt = 36

V_rlt = 37

V_rlt = 38

V_rlt = 39

V_rlt = 40

V_rlt = 41

V_rlt = 42

V_rlt = 43

V_rlt = 44

V_rlt = 45

V_rlt = 46

V_rlt = 47

V_rlt = 48

V_rlt = 49

V_rlt = 50

V_rlt = 51

V_rlt = 52

V_rlt = 53

V_rlt = 54

V_rlt = 55

V_rlt = 56

V_rlt = 57

V_rlt = 58

V_rlt = 59

V_rlt = 60

V_rlt = 61

V_rlt = 62

V_rlt = 63

V_rlt = 64

V_rlt = 65

V_rlt = 66

V_rlt = 67

V_rlt = 68

V_rlt = 69

V_rlt = 70

V_rlt = 71

V_rlt = 72

V_rlt = 73

V_rlt = 74

V_rlt = 75

V_rlt = 76

V_rlt = 77

V_rlt = 78

V_rlt = 79

V_rlt = 80

V_rlt = 81

V_rlt = 82

V_rlt = 83

V_rlt = 84

V_rlt = 85

V_rlt = 86

V_rlt = 87

V_rlt = 88

V_rlt = 89

V_rlt = 90

V_rlt = 91

V_rlt = 92

V_rlt = 93

V_rlt = 94

V_rlt = 95

V_rlt = 96

V_rlt = 97

V_rlt = 98

V_rlt = 99

V_rlt = 100

The value of the variable is already greater than 100, and the current value is 101

The LOOP cycle is over!

PL/SQL procedure successfully completed.

2. WHILE...LOOP structure

Unlike the basic LOOP statement, the WHILE...LOOP structure can end the LOOP loop itself. The WHILE keyword needs to be followed by a Boolean expression. When the Boolean expression after WHILE is TRUE, the statement sequence of cyclic weight is executed once, and then it will re-determine whether the expression after WHILE is TRUE. The entire LOOP loop ends only when the Boolean expression after WHILE is FALSE.

The relevant syntax of the statement structure is as follows: []

WHILE boolean_expression

LOOP

Statement...

END LOOP [label_name]

[syntax description]

Boolean_expression: Boolean expression.

Statement: a sequence of statements that can be executed when boolean_expression is TRUE.

SQL > declare

2 v_rlt number (3): = 1

3 begin

four

5 while (v_rlt

< 100) 6 loop 7 dbms_output.put_line('v_rlt = '||v_rlt); 8 v_rlt:=v_rlt+1; 9 end loop; 10 dbms_output.put_line('while循环已经结束!'); 11 end; 12 / v_rlt = 1 v_rlt = 2 v_rlt = 3 v_rlt = 4 v_rlt = 5 v_rlt = 6 v_rlt = 7 v_rlt = 8 v_rlt = 9 v_rlt = 10 v_rlt = 11 v_rlt = 12 v_rlt = 13 v_rlt = 14 v_rlt = 15 v_rlt = 16 v_rlt = 17 v_rlt = 18 v_rlt = 19 v_rlt = 20 v_rlt = 21 v_rlt = 22 v_rlt = 23 v_rlt = 24 v_rlt = 25 v_rlt = 26 v_rlt = 27 v_rlt = 28 v_rlt = 29 v_rlt = 30 v_rlt = 31 v_rlt = 32 v_rlt = 33 v_rlt = 34 v_rlt = 35 v_rlt = 36 v_rlt = 37 v_rlt = 38 v_rlt = 39 v_rlt = 40 v_rlt = 41 v_rlt = 42 v_rlt = 43 v_rlt = 44 v_rlt = 45 v_rlt = 46 v_rlt = 47 v_rlt = 48 v_rlt = 49 v_rlt = 50 v_rlt = 51 v_rlt = 52 v_rlt = 53 v_rlt = 54 v_rlt = 55 v_rlt = 56 v_rlt = 57 v_rlt = 58 v_rlt = 59 v_rlt = 60 v_rlt = 61 v_rlt = 62 v_rlt = 63 v_rlt = 64 v_rlt = 65 v_rlt = 66 v_rlt = 67 v_rlt = 68 v_rlt = 69 v_rlt = 70 v_rlt = 71 v_rlt = 72 v_rlt = 73 v_rlt = 74 v_rlt = 75 v_rlt = 76 v_rlt = 77 v_rlt = 78 v_rlt = 79 v_rlt = 80 v_rlt = 81 v_rlt = 82 v_rlt = 83 v_rlt = 84 v_rlt = 85 v_rlt = 86 v_rlt = 87 v_rlt = 88 v_rlt = 89 v_rlt = 90 v_rlt = 91 v_rlt = 92 v_rlt = 93 v_rlt = 94 v_rlt = 95 v_rlt = 96 v_rlt = 97 v_rlt = 98 v_rlt = 99 while循环已经结束! PL/SQL procedure successfully completed. 3.FOR...LOOP结构 FOR...LOOP语句可以遍历某个范围的整数,该范围被FOR和LOOP关键词封闭。首次进入循环时,循环范围将被确定,并且以后不会再次计算。每循环一次,循环指数将会自动增加1。 FOR...LOOP语句的语法结构如下 [] FOR index_name IN [ REVERSE ] lower_bound .. upper_bound LOOP statement... END LOOP [label_name]; 【语法说明】 index_name:循环计数器,是一个变量,它可以得到当前的循环指数。需要注意的是,不能为其手工赋值。 REVERSE:可选项,指定循环方式。默认的循环方式由下标(lower_bound)到上标(upper_bound)。使用该选项则从上标界到下标界。 lower_bound:循环范围的下标界。 upper_bound:循环范围的上标界。 下标和上标之间的".."不能省略。 SQL>

Declare

2 v_rlt number (3): = 1

3 begin

4 for v_rlt in 1..100 loop

5 dbms_output.put_line ('v_rlt =' | | v_rlt)

6 end loop

7 dbms_output.put_line ('for cycle is over!')

8 end

9 /

V_rlt = 1

V_rlt = 2

V_rlt = 3

V_rlt = 4

V_rlt = 5

V_rlt = 6

V_rlt = 7

V_rlt = 8

V_rlt = 9

V_rlt = 10

V_rlt = 11

V_rlt = 12

V_rlt = 13

V_rlt = 14

V_rlt = 15

V_rlt = 16

V_rlt = 17

V_rlt = 18

V_rlt = 19

V_rlt = 20

V_rlt = 21

V_rlt = 22

V_rlt = 23

V_rlt = 24

V_rlt = 25

V_rlt = 26

V_rlt = 27

V_rlt = 28

V_rlt = 29

V_rlt = 30

V_rlt = 31

V_rlt = 32

V_rlt = 33

V_rlt = 34

V_rlt = 35

V_rlt = 36

V_rlt = 37

V_rlt = 38

V_rlt = 39

V_rlt = 40

V_rlt = 41

V_rlt = 42

V_rlt = 43

V_rlt = 44

V_rlt = 45

V_rlt = 46

V_rlt = 47

V_rlt = 48

V_rlt = 49

V_rlt = 50

V_rlt = 51

V_rlt = 52

V_rlt = 53

V_rlt = 54

V_rlt = 55

V_rlt = 56

V_rlt = 57

V_rlt = 58

V_rlt = 59

V_rlt = 60

V_rlt = 61

V_rlt = 62

V_rlt = 63

V_rlt = 64

V_rlt = 65

V_rlt = 66

V_rlt = 67

V_rlt = 68

V_rlt = 69

V_rlt = 70

V_rlt = 71

V_rlt = 72

V_rlt = 73

V_rlt = 74

V_rlt = 75

V_rlt = 76

V_rlt = 77

V_rlt = 78

V_rlt = 79

V_rlt = 80

V_rlt = 81

V_rlt = 82

V_rlt = 83

V_rlt = 84

V_rlt = 85

V_rlt = 86

V_rlt = 87

V_rlt = 88

V_rlt = 89

V_rlt = 90

V_rlt = 91

V_rlt = 92

V_rlt = 93

V_rlt = 94

V_rlt = 95

V_rlt = 96

V_rlt = 97

V_rlt = 98

V_rlt = 99

V_rlt = 100

The for cycle is over!

PL/SQL procedure successfully completed.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report