oracle for in 学习

oracle for  in 是对于进行循环的数据处理时比较方便的

因为我们平时的操作经常会碰到进行循环的数据操作

以下为建立的例子

1.

begin

for item in 2..10 loop

 dbms_output.put_line('the reuslt is '||item);
end loop;

end;

输出的结果如下:

the reuslt is 2
the reuslt is 3
the reuslt is 4
the reuslt is 5
the reuslt is 6
the reuslt is 7
the reuslt is 8
the reuslt is 9
the reuslt is 10

2. 进行数据库相关的操作

创建的表如下:

 CREATE TABLE "APPSERVERUSER"."FIRSTCLASS"
   ( "ID" VARCHAR2(20 BYTE),
 "NAME" VARCHAR2(20 BYTE),
 "USERID" VARCHAR2(20 BYTE)
   )

进行的for loop 循环的操作如下:

begin

begin
for i in (select id from firstclass) loop 
    dbms_output.put_line(i.id);
end loop;
end;

注意此时的i 类似于oracle 的record 即一条记录

所以我们在使用的时候应该是:i.id

操作的输出结果如下:

the firstclass id is :1
the firstclass id is :2
the firstclass id is :3
the firstclass id is :4
the firstclass id is :5

灵活的使用操作语句对于我们的日常操作可以提供很多便捷的方式。

end; 

原文地址:https://www.cnblogs.com/rongfengliang/p/3709061.html