首页 > 数据库 > Oracle > Oracle Procedure IV
处理单行数据用 select .. into
处理多行数据就必须需要使用到游标了,游标是一个 SQL 的结果集
处理多行数据游标必须和循环结合使用。
--- 显示游标 ---
|
1
2
3
4
5
6
7
8 |
declare
cursor
cur_emp
is
select
*
from
emp;begin
for
i
in
cur_emp loop
dbms_output.put_line(i.ename ||
' '
|| i.sal);
end
loop;end
; |
工作中一般都用显示游标
--- 隐式游标 ---
|
1
2
3
4
5 |
begin
for
i
in
(
select
*
from
emp) loop
dbms_output.put_line(i.ename ||
' '
|| i.sal);
end
loop;end
; |
将游标应用到之前的控制判断工资水平中,输出多行工资水准:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
declare
cursor
cur_emp
is
select
*
from
emp;begin
for
i
in
cur_emp loop
case
when
i.sal >= 700
and
i.sal <= 1200
then
dbms_output.put_line(i.empno ||
' '
|| i.ename ||
' 工资真他妈低,还要不要人活了'
);
when
i.sal > 1200
and
i.sal <= 1400
then
dbms_output.put_line(i.empno ||
' '
|| i.ename ||
' 工资太他妈低了'
);
when
i.sal > 1400
and
i.sal <= 2000
then
dbms_output.put_line(i.empno ||
' '
|| i.ename ||
' 工资很低'
);
when
i.sal > 2000
and
i.sal <= 3000
then
dbms_output.put_line(i.empno ||
' '
|| i.ename ||
' 刚好够生活'
);
when
i.sal > 3000
and
i.sal <= 9999
then
dbms_output.put_line(i.empno ||
' '
|| i.ename ||
' 哥还是很牛逼的'
);
end
case
;
end
loop;end
; |
效果如下所示:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70013625/viewspace-3087080/ ,如需转载,请注明出处,否则将追究法律责任。