cursor : 普通,带参,可更新的游标。使用游标遍历时,强烈建议用for循环!!!
cursor:
源数据表account中仅有两条记录:

如果输出在判断前,则出错,将最后一条记录输出两次,如下:

所以,一定要先判断notfound再输出结果:

exit when (c%notfound);
dbms_output.put_line(v_acc.id);
强烈建议使用for 循环! 简单!不易出错!
简单:1.v_acc 不用在循环外声明
2.省去了fetch,open,close 操作。for内部自动完成。
不易出错:
只有输出语句,不用再思考fetch,notfound/found,output之间的前后逻辑关系,傻瓜操作,出错都难!

在pl/sql developer软件中要用command窗口操作:

带参数的游标使用如下:
vtmp 可以不声明 --vtmp c%rowtype; (for循环中in cursorName即自动设置了vtmp的类型,for循环外部无需声明)

这一点类似于JAVA的增强for循环:
下面例子摘录自:http://www.cnblogs.com/mengdd/archive/2013/01/21/2870019.html
int [] arr={0,1,2,3,4,5};
for(int element:arr)
{
System.out.println(element);
}
//遍历二维数组
int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ;
for(int[] row : arr2)
{
for(int element : row)
{
System.out.println(element);
}
}
//以三种方式遍历集合List
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println("----------方式1-----------");
//第一种方式,普通for循环
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
System.out.println("----------方式2-----------");
//第二种方式,使用迭代器
for(Iterator<String> iter = list.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
System.out.println("----------方式3-----------");
//第三种方式,使用增强型的for循环
for(String str: list)
{
System.out.println(str);
}
}
}
For-Each循环的缺点:丢掉了索引信息。
当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。
可更新游标:注意与JAVA不同的语法(赋值为:= 判断相等为单个= 条件判断为if then elsif then end if )


浙公网安备 33010602011771号