Presentation is loading. Please wait.

Presentation is loading. Please wait.

第四章 PL/SQL控制结构 If-then:如果条件为true,则执行一行或多行代码,如果条件为假,转到end if之后。

Similar presentations


Presentation on theme: "第四章 PL/SQL控制结构 If-then:如果条件为true,则执行一行或多行代码,如果条件为假,转到end if之后。"— Presentation transcript:

1 第四章 PL/SQL控制结构 If-then:如果条件为true,则执行一行或多行代码,如果条件为假,转到end if之后。
4.1 条件控制 IF-THEN语句 If-then:如果条件为true,则执行一行或多行代码,如果条件为假,转到end if之后。 例:if var1>10 then var2:=var1+20; end if If语句允许嵌套,如: If var1>10 then if var2<var1 then end if; End if; 规则1:每个if语句都有自己的then,以if开始的语句行不跟; 规则2:每个if语句块以相应的end if结束。

2 4.1.2 IF-THEN-ELSE语句 例: If var1>10 then var2:=var1+20; Else
var2:=var1*var1; End if; If-then-else语句也可以嵌套。例: if var1 between 7 and 8 then var2:=2*var1; else end if;

3 4.1.3 IF-THEN-ELSIF语句 这种结构用于替代嵌套if-then-else结构。上例可改写为:
规则3:每个if语句有且只有一个else。 规则4:else语句行不跟;   IF-THEN-ELSIF语句 这种结构用于替代嵌套if-then-else结构。上例可改写为: If var1>10 then var2:=var1+20; Elsif var1 between 7 and 8 then var2:=2*var1; Else var2:=var1*var1; End if; 规则5:elseif无匹配的end if **编程采用缩进结构。

4 Else不是必须的。例: If location=‘phonex’ then v_hockey_team_name:=‘coyotes’; Elsif location=‘new york city’ then v_hockey_team_name:=‘ranges’; Elsif location:=‘ottawa’ then v_hockey_team_name:=‘senators’; End if; null条件:if-then-else语句的条件为true时,执行then后的语句,条件为false或null时,执行else后的语句。考虑两个块:

5 /* block1 */ Declare v_number1 number; v_number2 number; v_result varchar2(7); Begin if v_number1<v_number2 then v_result:=‘yes’; else v_result:=‘no’; end if; End;

6 /* block2 */ Declare v_number1 number; v_number2 number; v_result varchar2(7); Begin if v_number1>=v_number2 then v_result:=‘no’; else v_result:=‘yes’; end if; End; 当v_number1和v_number2的值非null时,这两个块等价,但是,当其中之一为null时,这两个块就不同。如v_number1=3,v_number2为null,块1的结果为no,块2的结果为yes。

7 如果对上面两个块添加对null的检测,则这两个块等价。
/* block1 */ Declare v_number1 number; v_number2 number; v_result varchar2(7); Begin if v_number1 is null or v_number2 is null then v_result:=‘unknown’; elsif v_number1<v_number2 then v_result:=‘yes’; else v_result:=‘no’; end if; End;

8 /* block2 */ Declare v_number1 number; v_number2 number; v_result varchar2(7); Begin if v_number1 is null or v_number2 is null then v_result:=‘unknown’; elsif v_number1>=v_number2 then v_result:=‘no’; else v_result:=‘yes’; end if; End;

9 4.1.4 CASE语句 语法如下: Case selector
when expression1 then sequence_of_statement1; when expression2 then sequence_of_statement2; ……….. when expressionN then sequence_of_statementN; [else sequence_of_statementN+1;] End case 例: Declare grade char:=‘A’; begin if grade =‘A’ then dbms_output.put_line(‘Excellent’);

10 elsif grade=‘B’ then dbms_output.put_line(‘very good’); elsif grade=‘C’ then dbms_output.put_line(‘good’); elsif grade=‘D’ then dbms_output.put_line(‘fair’); elsif grade=‘F’ then dbms_output.put_line(‘poor’); else dbms_output.put_line(‘no such grade’); end if; End;

11 用case语句重写以上程序如下: Declare grade char:=‘A’; Begin case grade when ‘A’ then dbms_output.put_line(‘Excellent’); when ‘B’ then dbms_output.put_line(‘very good’); when ‘C’ then dbms_output.put_line(‘good’); when ‘D’ then dbms_output.put_line(‘fair’); when ‘F’ then dbms_output.put_line(‘poor’); else dbms_output.put_line(‘no such grade’); end case; End;

12 搜索CASE语句 语法如下 Case when search_condition1 then sequence_of_statement1; when search_condition2 then sequence_of_statement2; when search_conditionN then sequence_of_statementN; [else sequence_of_statementN+1;] END CASE 例:上例改写如下:

13 Declare grade char:=‘A’; Begin case when grade=‘A’ then bms_output.put_line(‘Excellent’); when grade=‘B’ then dbms_output.put_line(‘very good’); when grade=‘C’ then dbms_output.put_line(‘good’); when grade=‘D’ then dbms_output.put_line(‘fair’); when grade=‘F’ then dbms_output.put_line(‘poor’); else dbms_output.put_line(‘no such grade’); end case; End;

14 Loop-exit-end循环:此循环有三部分组成,见下例
4.2 循环结构 LOOP循环 Loop-exit-end循环:此循环有三部分组成,见下例 Cnt:=1; Loop cnt:=cnt+1; if cnt>=100 then exit; end if; End loop; Loop-exit-when-end循环:例 Declare cnt binary_integer:=1; insert into temp_table values (cnt,’loop index’); exit when cnt>=100; end loop; End;

15 4.2.2 WHILE-LOOP循环 在while部分测试退出条件。例 Declare cnt binary_integer:=1;
while cnt<=100 loop insert into temp_table values (cnt,’loop index’); cnt:=cnt+1; end loop; End;

16 FOR循环 语法如下: For loop_counter in [reverse] low_bound ..high_bound loop .. End loop; 这里loop_counter是隐式声明的索引变量。例: Begin for v_counter in loop insert into temp_table values (v_counter,’loop index’); end loop; End; 域法则:for循环的循环索引被隐式声明为binary_integer,在循环前面没有必要声明他。如果对他进行了声明,那么循环索引将隐蔽外层的声明。正如内部块的声明会隐蔽外部块的声明一样。

17 如: Declare v_counter number:=7; Begin insert into temp_table(num_col) values (v_counter); for v_counter in loop end loop; End;

18 使用reverse:如果在for循环中有reverse关键字,则循环索引将从最大值向最小值进行迭代。仍然是先写小值,后写大值。
Begin for v_counter in reverse loop null; end loop; End; 循环范围:最大值和最小值没有必要是数值文字,他们可以是能够被转换为数字值的任何表达式。例: Declare v_lowervalue number:=10; v_highvalue number:=40; for v_counter in v_lowervalue ..v_highvalue loop insert into temp_table values (v_counter,’dynamically specified loop range’);

19 循环语句使用指导

20 4.3 顺序结构 4.3.1 GOTO语句 语法如下: Goto label;
4.3 顺序结构 GOTO语句 语法如下: Goto label; Label是用<<>>括起来定义的标号。 例: Declare v_counter binary_integer :=1; Begin loop insert into temp_table values (v_counter,’loop count’); v_count:=v_count+1; if v_count>50 then goto l_endofloop; end if; end loop; <<l_endofloop>> insert into temp_table (char_col) values “done”; End;

21 1.对于goto的限制:对于块、循环、if语句,不能从外层转到内层。下例是非法的:
begin goto l_innerblock; <<l_innerblock>> end; goto l_insideif; if x>3 then <<l_insideif>> insert into … end if; End;

22 使用goto语句从一个if语句块转到另一个子句也是非法的。
例: Begin if x>3 then goto l_nextcondition; else <<l_nextcondition>> end if; End;

23 从一个异常处理块的内部跳到当前执行块是非法的。
例: Declare select * into v_room from rooms where roomid=1; <<l_insert>> insert into temp_table (char_col) values (‘found a row’); Exception when no_data_found yhen goto l_insert; End; 2.为循环设定标签 循环可以设定标签,在exit语句中可以使用标签指明退出退出哪个循环。

24 例: Begin <<l_outer>> for v_outerindex in loop <<l_inner>> for v_innerindex in loop if v_innerindex>40 then exit l_outer; end if; end loop l_inner; end loop l_out; End;

25 4.3.2 NULL语句 如果想显式第指明进行任何操作,可以使用null语句,他只是一个占位符。例 Declare
v_tempvar number :=7; Begin if v_tempvar<5 then insert into temp_table (char_col) values (‘too small’); elsif v_numvar<10 then insert into temp_table(char_col) values (‘just right’); else null; end if; End;


Download ppt "第四章 PL/SQL控制结构 If-then:如果条件为true,则执行一行或多行代码,如果条件为假,转到end if之后。"

Similar presentations


Ads by Google