Oracle流程控制语句

时间:2021-08-14 14:48:03

1.选择语句

1.1 IF...THEN...END IF语句

DECLARE MY_AGE INT;
IF MY_AGE IS NULL THEN
DBMS_OUTPUT.put_line('AGE IS NULL');
END IF;

  

1.2 IF...THEN ELSIF THEN....ELSE THEN... ENDIF语句,这里需要注意的是ELSIF这个,JAVA里面的写法就是 else if  这里是ELSIF,IF前面不能有空格。

IF AGE <= 6 AND AGE > 0 THEN
DBMS_OUTPUT.put_line('CHILD');
ELSIF AGE <= 17 AND AGE > 6 THEN
DBMS_OUTPUT.put_line('TEENAGER');
ELSIF AGE <= 40 AND AGE > 17 THEN
DBMS_OUTPUT.put_line('YOUTH');
ELSIF AGE <= 65 AND AGE > 40 THEN
DBMS_OUTPUT.put_line('Middle-aged');
ELSIF AGE > 65 AND AGE<= 150 THEN
DBMS_OUTPUT.put_line('OLDER');
ELSE
DBMS_OUTPUT.put_line('YOU AGE IS OUT OF SPACE');
END IF;

1.3.CASE 语句

CASE AGE
WHEN 1 THEN DBMS_OUTPUT.put_line('BABY');
WHEN 15 THEN DBMS_OUTPUT.put_line('TEENAGER');
WHEN 25 THEN DBMS_OUTPUT.put_line('ADULT');
ELSE DBMS_OUTPUT.put_line('OLDER');
END CASE;
END;

2.循环语句

2.1 LOOP 语句

DECLARE RESULT NUMBER:=0;
NUM1 NUMBER:=1;
NUM2 NUMBER:=100; LOOP
RESULT:=RESULT+NUM1;
NUM1:=NUM1+1;
EXIT WHEN NUM1>NUM2;
END LOOP; DBMS_OUTPUT.put_line('LOOP RESUT IS:'||RESULT);

2.2 WHILE语句

 WHILE NUM1<=NUM2 LOOP
RESULT:=RESULT+NUM1;
NUM1:=NUM1+1;
END LOOP;
DBMS_OUTPUT.put_line(RESULT);

2.3 FOR语句

FOR J IN REVERSE NUM1..NUM2 LOOP
RESULT:=RESULT+J;
DBMS_OUTPUT.put_line(J);
END LOOP;
DBMS_OUTPUT.put_line(RESULT);