简单LOOP语句
语法图
示例
```
CREATE OR REPLACE PROCEDURE proc_loop(i in integer, count out integer)
AS
BEGIN
count:=0;
LOOP
IF count > i THEN
raise info 'count is %. ', count;
EXIT;
ELSE
count:=count+1;
END IF;
END LOOP;
END;
/
CALL proc_loop(10,5); ```
须知:
该循环必须要结合EXIT使用,否则将陷入死循环。
WHILE_LOOP语句
语法图
只要条件表达式为真,WHILE语句就会不停的在一系列语句上进行循环,在每次进入循环体的时候进行条件判断。
示例
```
CREATE TABLE integertable(c1 integer) ;
CREATE OR REPLACE PROCEDURE proc_while_loop(maxval in integer)
AS
DECLARE
i int :=1;
BEGIN
WHILE i < maxval LOOP
INSERT INTO integertable VALUES(i);
i:=i+1;
END LOOP;
END;
/
--调用函数 CALL proc_while_loop(10);
--删除存储过程和表 DROP PROCEDURE proc_while_loop; DROP TABLE integertable; ```
FOR_LOOP(integer变量)语句
语法图
说明:
- 变量name会自动定义为integer类型并且只在此循环里存在。变量name介于lower_bound和upper_bound之间。
- 当使用REVERSE关键字时,lower_bound必须大于等于upper_bound,否则循环体不会被执行。
FOR_LOOP查询语句
语法图
说明:
变量target会自动定义,类型和query的查询结果的类型一致,并且只在此循环中有效。target的取值就是query的查询结果。
FORALL批量查询语句
语法图
说明:
变量index会自动定义为integer类型并且只在此循环里存在。index的取值介于low_bound和upper_bound之间。
示例
``` CREATE TABLE hdfs_t1 ( title NUMBER(6), did VARCHAR2(20), data_peroid VARCHAR2(25), kind VARCHAR2(25), interval VARCHAR2(20), time DATE, isModified VARCHAR2(10) )
INSERT INTO hdfs_t1 VALUES( 8, 'Donald', 'OConnell', 'DOCONNEL', '650.507.9833', to_date('21-06-1999', 'dd-mm-yyyy'), 'SH_CLERK' );
CREATE OR REPLACE PROCEDURE proc_forall() AS BEGIN FORALL i IN 100..120 update hdfs_t1 set title = title + 100*i; END; /
--调用函数 CALL proc_forall();
--查询存储过程调用结果 SELECT * FROM hdfs_t1 WHERE title BETWEEN 100 AND 120;
--删除存储过程和表 DROP PROCEDURE proc_forall; DROP TABLE hdfs_t1; ```









