1. 条件分支语句
<1>if语句
<span style="font-size: 14px;">
<span style="font-family: Arial, Helvetica, sans-serif;">DECLARE</span>
in_var emp.sal%TYPE;
BEGIN
SELECT sal INTO in_var
FROM emp
WHERE empno=&out_var;
IF(in_var<=2000)THEN
dbms_output.put_line('in_var<=2000.');
ELSIF in_var<=5000 AND in_var>2000 THEN --!
dbms_output.put_line('in_var<=5000 AND in_var>2000.');
ELSE
dbms_output.put_line('in_var>5000.');
END IF;
END;
</span>
注:此处与其他语言的if语句略有差异,在Oracle数据库中,if语句的其他条件,使用elsif关键字,而不是else if。
<2>case语句
case语句可根据条件从多个执行分支选择相应的执行动作,并返回一个值。语法如下:
CASE selector
WHEN expression1 Thenresult1
WHENexpressionn Thenresultn
[ ELSEresultn+1; ]
END;
用case语句解决上面案例,如下:
<span style="font-size:14px;">
DECLARE
in_var emp.sal%TYPE;
msg_var VARCHAR2(32767) DEFAULT ''; --(length range:1-32767)
BEGIN
SELECT sal INTO in_var
FROM emp
WHERE empno=&out_var;
msg_var:=
CASE
WHEN in_var<=2000 THEN 'in_var<=2000.'
WHEN in_var>2000 AND in_var<=5000 THEN '2000<in_var<=5000.'
ELSE
'in_var>5000.'
END;
dbms_output.put_line('msg_var:'||msg_var);
END;
</span>
selector语句可以省略,如下:
<span style="font-size:14px;">
DECLARE
v_var VARCHAR2(10);
v_deptno emp.deptno%TYPE;
BEGIN
SELECT deptno INTO v_deptno
FROM emp
WHERE sal=(SELECT MAX(sal)FROM emp);
v_var:=
CASE v_deptno
WHEN 10 THEN '部门一'
WHEN 20 THEN '部门二'
ELSE '部门三'
END;
dbms_output.put_line(v_var);
END;
</span>
2.循环语句
<1>basic loop(无条件循环,循环体至少执行一次,须有exit语句)
语法如下:
LOOP
statement1;
...
EXIT [WHEN condition];
END LOOP;
<pre name="code" class="sql">/* 使用循环插入多条数据 */
<span style="font-size:14px;">
DECLARE
v_count NUMBER(2):=1;
v_total NUMBER(2):=5;
v_empno emp_v1.empno%TYPE;
v_ename emp_v1.ename%TYPE:='zl';
v_job emp_v1.job%TYPE:='nurse';
BEGIN
SELECT MAX(empno)INTO v_empno
FROM emp_v1;
IF v_empno IS NULL THEN
v_empno:=0;
END IF;
dbms_output.put_line('last v_empno:'||v_empno);
LOOP
INSERT INTO emp_v1(empno,ename,job)
VALUES(v_empno+v_count,v_ename || v_count,v_job);
v_count:=v_count+1;
EXIT WHEN v_count>v_total;
END loop;
END;
</span>
<2>For Loop
<span style="font-size:14px;">
DECLARE
v_empno emp_v1.empno%TYPE;
v_ename emp_v1.ename%TYPE:='zw';
v_job emp_v1.job%TYPE:='doctor';
BEGIN
DELETE FROM emp_v1;
SELECT MAX(empno) INTO v_empno FROM emp_v1;
FOR counter IN 1..5 LOOP
INSERT INTO emp_v1(empno,ename,job)
VALUES(NVL(v_empno,0)+counter,v_ename||counter,v_job);
END LOOP;
END;
</span>
此处需要注意,在for-loop循环中,计数器counter不需要我们声明,循环的下界不能大于上界,否则,sql语句不会报错,但是也不会执行。
<3>while loop
<span style="font-size:14px;">
DECLARE
v_count NUMBER(2):=1;
v_empno emp_v1.empno%TYPE;
v_ename emp_v1.ename%TYPE:='hjt';
v_job emp_v1.job%TYPE:='officer';
BEGIN
SELECT MAX(empno) INTO v_empno FROM emp_v1;
WHILE v_count<=3 LOOP
INSERT INTO emp_v1(empno,ename,job)
VALUES(NVL(v_empno,0)+v_count,v_ename||v_count,v_job);
v_count:=v_count+1;
END LOOP;
END;
</span>
<span style="font-size:14px;">
BEGIN
dbms_output.put_line('this is a map!');
dbms_output.put_line('wecome to china!');
GOTO pointer_one;
dbms_output.put_line('hello ,everyone!');
<<pointer_one>>
dbms_output.put_line('excuse me!');
END;
</span>
注,goto语句不能跳转到嵌套块内部的标签,也不能从if子句外面跳转到if子句内部,也不能从一个if子句中跳转到另一个if子句中,也不能从异常处理部分跳转到代码其他部分。