oracle 游标
来源:互联网 发布:电线电缆计算软件 编辑:程序博客网 时间:2024/05/05 01:16
--1.静态游标:编译时才确定elect语句。
--1)隐式游标:用户不能直接控制的静态游标(自动开,自动取,自动关)
--当用户用update,delete,insert,select(带into)时,自动产生隐式游标。
--游标名字为:sql
--相关属性:%found,%notfound,%isopen,%rowcount.
--exp 1.1.1:
begin
update emp set sal=sal+1 where empno > &emp_no;
if sql%found then
dbms_output.put_line('更新了'||sql%rowcount||'行');
else
dbms_output.put_line('没找到');
end if;
end;
--exp 1.1.2:
declare
emp_row emp%rowtype;
begin
select * into emp_row from emp where ename like '&e_name';
if sql%found then
dbms_output.put_line('编号:'||emp_row.empno||' 名字:'||emp_row.ename);
end if;
exception
when no_data_found then
dbms_output.put_line('没有找到');
when too_many_rows then
dbms_output.put_line('太多了');
when others then
dbms_output.put_line('莫名其妙');
end;
--2)显示游标:用户要自己控制的静态游标
--操作步骤:
--a)声明游标 b)打开游标 c)取记录 d)关游标
--exp 1.2.1:显示游标的查询
declare
emp_row emp%rowtype;
cursor emp_cur is select * from emp; --a)
begin
open emp_cur; --b)
loop
fetch emp_cur into emp_row; --c)
exit when emp_cur%notfound;
dbms_output.put_line('编号:'||emp_row.empno||' 名字:'||emp_row.ename);
end loop;
close emp_cur; --d)
end;
--exp 1.2.2:显示游标的更新
declare
emp_row emp%rowtype;
cursor emp_cur is select * from emp where empno>&emp_no for update;
begin
open emp_cur;
loop
fetch emp_cur into emp_row;
exit when emp_cur%notfound;
update emp set sal=sal+100 where current of emp_cur;
dbms_output.put_line('更新了编号为'||emp_row.empno||'姓名是'||emp_row.ename||'的员工,更新后工资为'||emp_row.sal||'元');
end loop;
close emp_cur;
commit;
end;
--2.ref游标:运行时才确定select语句.
--由游标类型和游标变量组成。
--游标类型:
--1)强类型:指的是在声明游标类型时带return type,这样在游标取值时只能取相对应的类型。
--2)弱类型:不带return type,这样在取值时不论什么值都能取。
--游标变量:声明为游标类型的变量。
--exp:2.2.1
declare
type emp_cur_type is ref cursor;
emp_cur emp_cur_type;
emp_row emp%rowtype;
begin
open emp_cur for
'select * from emp';
loop
fetch emp_cur into emp_row;
exit when emp_cur%notfound;
dbms_output.put_line('编号:'||emp_row.empno||' 姓名:'||emp_row.ename);
end loop;
close emp_cur;
end;
--exp:2.2.2:带参数的ref游标
declare
type emp_cur_type is ref cursor;
emp_cur emp_cur_type;
emp_row emp%rowtype;
e_name emp.ename%type default '&e_name';
begin
open emp_cur for
'select * from emp where ename like :1'
using e_name;
loop
fetch emp_cur into emp_row;
exit when emp_cur%notfound;
dbms_output.put_line('编号:'||emp_row.empno||'姓名:'||emp_row.ename);
end loop;
close emp_cur;
end;
--3.循环游标
--特性:
--a)在游标中取完记录后自动停止。(即,不需要exit when)
--b)自动提取记录。(即:不用fetch into)
--c)自动关闭游标。(即:不用close)
declare
cursor emp_cur is
select * from emp;
begin
for emp_row in emp_cur
loop
dbms_output.put_line('编号:'||emp_row.empno||' 姓名:'||emp_row.ename);
end loop;
end;
- Oracle游标
- Oracle游标
- ORACLE 游标
- Oracle 游标
- oracle 游标
- oracle 游标
- oracle 游标
- Oracle游标
- ORACLE游标
- Oracle游标
- Oracle 游标
- Oracle 游标
- ORACLE游标
- Oracle 游标
- oracle 游标
- Oracle 游标
- oracle游标
- ORACLE 游标
- C语言总结-个人学习笔记
- 嘛...又一同事辞职...
- ASP.NET MVC preview 1升级到ASP.NET MVC preview 2的一些更改
- oracle 触发器 和 常用内置程序包
- 超文本传输协议(HTTP/1.1)中文翻译
- oracle 游标
- 调试ASP遇到的一些问题的解决心得
- [tamarin系列之4] 构建专用的avmshell
- 清风箴言 永远Beta版
- 带命令行参数的fibonacci(YC)
- 操作系统不能运行EXE程序
- 疯狂代码生成器设计方案
- 李银河经典语录集锦
- 本科女生求职记