-----------------------------------编写控制结构----------------------------- --条件分支判断 简单条件判断 IF CONDITION THEN statement; END IF; 二重条件分支 IF CONDITION THEN statement; ELSE statement; END IF; 多重条件分支 IF CONDITION THEN statement; ELSIF CONDITION THEN statement; ELSE statement; END IF; declare v_job varchar2(10); v_sal int; begin select job,sal into v_job,v_sal from emp where empno=&no; IF v_job = 'PERSIDENT' THEN UPDATE emp set sal = v_sal + 1000 where empno=&no; ELSIF v_job = 'MANAGER' THEN UPDATE emp set sal = v_sal + 500 where empno=&no; ELSE UPDATE emp set sal = v_sal +1000 where empno=&no; END IF; END; select * from emp; --CASE语句 1:在case中使用单一选择符进行比较等值 CASE selector when expression1 then sequence_of_statement1; when expression2 then sequence_of_statement2; when expression3 then sequence_of_statement3; when expressionN then sequence_of_statementN; [ELSE sequence_of_statementN+1] END CASE; declare v_deptno emp.deptno%type; begin v_deptno :=&no; CASE v_deptno WHEN 10 THEN UPDATE emp set common=100 where deptno=v_deptno; when 20 then UPDATE emp set common=100 where deptno=v_deptno; when 30 then UPDATE emp set common=100 where deptno=v_deptno; else dbms_output.put_line('不存在该部门'); END CASE; END; / 2:在case语句中使用多条件比较 CASE selector 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; declare v_sal emp.sal%type; v_ename emp.ename%type; begin select ename,sal into v_ename,v_sal from emp where empno=&no; case when v_sal < 1000 then update emp set common=100 where ename=v_ename; when v_sal < 2000 then update emp set common=200 where ename = v_ename; when v_sal > 6000 then update emp set common=300 where ename = v_ename; else dbms_output.put_line('不存在员工'); end case; end; --循环语句 1:基本循环 LOOP statement1; .. EXIT [WHEN condition]; END LOOP; 将1..10的整数插入到temp表 create table temp(cola int); select * from temp declare i int :=1; begin loop insert into temp values(i); exit when i=10; i :=i+1; end loop; end; --WHILE 循环 WHILE condition LOOP statement1; statement2; .. END LOOP; declare i int :=11; begin while i < 20 loop insert into temp values(i); i := i+1; end loop; end; --FOR循环 FOR counter in [REVERSE] lower_bound..upper_bound loop statement1; statement2; .. end loop; begin for i in reverse 20..30 loop insert into temp values(i); end loop; end; --嵌套循环和标记