//author 满晨晨
//time 2009 4 25上午
游标 存储过程 视图 触发器 数据库实际应用中非常重要的四部分
cursor 是系统为用户开设的一个数据缓冲区 存放sql语句的执行结果
每个游标都有自己的名字 用户可以用sql语句逐以从游标中获取记录
并赋给主变量 交给主语言进一步处理
主语言是面向记录的 一组主变量一次只能存放一条记录 仅用主变量并不能完全满足sql语句向应用程序输出数据的要求嵌入式sql引入了游标的概念 用来协调这两种不同的处理方式
cursor :
resultsset 结果集
recordposi 记录位置
本质上 游标实际上时一种从包括多条数据记录的结果集中每次提取一条记录的机制
游标总是与一条sql选择语句相关联 因为游标由结果集(可以使零条或者一条或者由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成
当决定对结果集进行处理时 必须声明一个指向该结果集的游标
游标分类
显示游标
用户自己声明操作
隐式游标
oracle为所有操作语言自动声明和操作的一种游标
显示游标
声明
cursor 游标名(变量名 数据类型,...)
return 返回类型
selectstatement;
打开
open 游标名(实参列表)
提取
fetch 游标名into(variable_list,record_variable)
关闭
close
%isopen
%found %notfound
%rowcount 返回当前位置为止游标读取的记录行数
set serveroutput on;
declare
t_empno emp.empno%type;
cursor mycursor is
select e.empno,e.ename from emp e where empno>t_empno;
cursored mycursor%rowtype;
begin
t_empno:=10;
open mycursor;
if mycursor%isopen then
fetch mycursor into cursored;
dbms_output.put_line(to_char(cursored.ename));
else
dbms_output.put_line('no open!!');
end if;
close mycursor;
end;
set serveroutput on;
declare
eno emp.empno%type;
e_name emp.ename%type;
cursor mycurs1(var_name varchar2) is
select e.empno,e.ename from emp e where e.ename like var_name//like '%'||var_name||'%';
begin
if mycurs1%isopen=false then
open mycurs1('%A%');
end if;
fetch mysurs1 into eno,e_ename;
while mysurs1%found loop
dbms_output.put_line(eno||':'||e_name);
if mycurs1%rowcount=4 then
exit;
end if;
fetch mycurs1 into eno,e_ename;
end loop;
close mycurs1;
end;
set serveroutput on;
declare
t_empno t_emp.empno%type;
cursor mycursor is
select e.empno,e.ename from t_emp e where empno>t_empno;
cursored mycursor%rowtype;
begin
t_empno:=1000;
open mycursor;
if mycursor%isopen then
fetch mycursor into cursored;
dbms_output.put_line(to_char(cursored.ename));
else
dbms_output.put_line('no open!!');
end if;
while mycursor%found
loop
dbms_output.put_line(t_empno||':'||to_char(cursored.ename));
if mycursor%rowcount=4 then
exit;
end if;
fetch mycursor into t_empno,cursored.ename;
end loop;
close mycursor;
end;
set serveroutput on;
declare
cursor mycurs1 is
select e.empno,e.ename,e.deptno from t_emp e where e.deptno=20;
begin
for employee in mycurs1
loop
if mycurs1%isopen then
dbms_output.put_line('学号:'||employee.empno||'姓名:'||employee.ename||'部门:'||employee.deptno);
end if;
end loop;
end;
/**
for
游标
**/
创建包
包
一般是过程和函数的集合,对过程和函数进行更好的封装,一般不针对字段
包的构成包括包头和包体
包头仅仅是针对包中过程或者函数进行说明
而没有实现
create or replace package pack_t1
is procedure sayhello(vname ,vachars);声明包的一个过程;
end;
包体是对包头中定义的过程函数的具体实现
create or replace package body pack_t1
is
procedure sayhello(vname varchar2);声明包的一个过程
is
begin
dbms_output.put_line('hello'||vname);
end;
end;
set serveroutput on;
declare
t_empno emp.empno%type;
cursor mycursor is
select e.empno,e.ename from emp e where empno>t_empno;
cursored mycursor%rowtype;
begin
t_empno:=10;
open mycursor;
if mycursor%isopen then
fetch mycursor into cursored;
dbms_output.put_line(to_char(cursored.ename));
else
dbms_output.put_line('no open!!');
end if;
close mycursor;
end;
set serveroutput on;
declare
eno emp.empno%type;
e_name emp.ename%type;
cursor mycurs1(var_name varchar2) is
select e.empno,e.ename from emp e where e.ename like var_name//like '%'||var_name||'%';
begin
if mycurs1%isopen=false then
open mycurs1('%A%');
end if;
fetch mysurs1 into eno,e_ename;
while mysurs1%found loop
dbms_output.put_line(eno||':'||e_name);
if mycurs1%rowcount=4 then
exit;
end if;
fetch mycurs1 into eno,e_ename;
end loop;
close mycurs1;
end;
set serveroutput on;
declare
t_empno t_emp.empno%type;
cursor mycursor is
select e.empno,e.ename from t_emp e where empno>t_empno;
cursored mycursor%rowtype;
begin
t_empno:=1000;
open mycursor;
if mycursor%isopen then
fetch mycursor into cursored;
dbms_output.put_line(to_char(cursored.ename));
else
dbms_output.put_line('no open!!');
end if;
while mycursor%found
loop
dbms_output.put_line(t_empno||':'||to_char(cursored.ename));
if mycursor%rowcount=4 then
exit;
end if;
fetch mycursor into t_empno,cursored.ename;
end loop;
close mycursor;
end;