Oracle实践--PL/SQL基础之游标
PL/SQL基础入门之游标
PL/SQL:过程语言(Procedure Language)和结构化语言(Structured Query Language)结合而成的编程语言,是对SQL的扩展,支持多种数据类型,如大对象和集合类型,可使用条件和循环等控制语句,可创建存储过程,程序包和触发器等,给sql语句的执行添加程序逻辑,与Oracle服务器和Oracle工具紧密集成,具有可移植性,灵活性和安全性。
--------------------------------------------------------------------------------------------------------------------------------------
/*
游标:可以用来存放查询结果,逐行提取查询结果,以编程的方式访问数据
类型:1.隐式游标;2.显示游标;3.引用游标
*/
/*
在PL/SQL中使用DML语句时自动创建隐式游标
隐式游标自动声明、打开和关闭,其名为 SQL
通过检查隐式游标的属性可以获得最近执行的DML语句的信息
隐式游标的属性有:
1.%FOUND – SQL语句影响了一行或多行时为 TRUE
2.%NOTFOUND – SQL语句没有影响任何行时为TRUE
3.%ROWCOUNT – SQL语句影响的行数
4.%ISOPEN -游标是否打开,始终为FALSE
*/
create or replace procedure sopV(obj varchar2) as --定义一个存储过程,用于输出字符串,简化书写 begin dbms_output.put_line(obj); end;
create or replace procedure sopN(obj number) as --定义一个存储过程,用于输出number类型 begin dbms_output.put_line(obj); end;--隐式游标1
declare v_ename emp.ename%type; v_sq lvarchar2(200); v_no number :=‘&编号:‘; begin v_sql :=‘select * from emp where empno =: no‘; execute immediate v_sql using v_no; if sql%notfound then--如果没有影响函数,notfound sopV(‘没有找到!‘); elsif sql%found then sopN(sql%rowcount); end if; end;
--隐式游标2
declare
v_name varchar2(20) :=‘ysjian‘;
begin
update emp set ename = v_name where empno =7369;
if sql%found then
sopV(‘更新成功‘);
sopN(sql%rowcount);
elsif sql%notfound then
sopV(‘没有找到!‘);
end if;
end;--隐式游标3declare
v_emp_rec emp%rowtype;
v_empno number :=‘&empno‘;
begin
select * into v_emp_rec from emp where empno=v_empno;
if sql%notfound
then dbms_output.put_line(‘没有找到‘);
else
dbms_output.put_line(v_emp_rec.empno||‘-->‘||v_emp_rec.ename);
end if;
exception
when no_data_found--异常类型,没有找到数据时
then dbms_output.put_line(‘data no found exception!‘);
end;
--显示游标1,关键字,cursor is opoen fetch close
declare
v_emp emp%rowtype;
cursor v_cur is--此处与一般的变量声明不同,类型发在名称前面,关键字不能用as,用is
select *from emp;
begin
open v_cur;--打开游标
loop
fetch v_cur into v_emp;--提取游标
sopV(v_emp.ename);
exit when v_cur%notfound;
end loop;
close v_cur;--关闭游标
end;
--显示游标2,for循环遍历,打开,提取和关闭游标的操作自动完成
declare
v_emp emp%rowtype;
cursor v_cur is select * from emp;
begin
for resin v_cur
loop
sopV(res.ename);
end loop;
end;
--显示游标3,带参数
declare
v_emp emp%rowtype;
cursor v_cur(parameter varchar2) is select * from emp where ename = parameter;
begin
for v_res in v_cur(‘&ename:‘)
loop
sopV(v_res.ename);
end loop;
end;
--显示游标4:用游标更新数据,关键字:select ... for update
select *from emp;
declare
v_salnumber;
cursor emp_cur is select * from emp where sal<1500 for update of sal; -- 更新sal字段
begin
for cin emp_cur
loop
v_sal := c.sal;
update emp set sal = v_sal*1.2 where current of emp_cur;-- 当前游标所指的行
end loop;
end;
-- REF游标:用于处理运行时动态地执行sql查询
declare
type emp_ref is ref cursor;--声明一个游标类型,此处又有不同哦
emp_cur emp_ref;--声明一个游标,类型是上面定义好的类型
v_emp emp%rowtype;
v_sal number :=‘&输入薪水:‘;
begin
open emp_cur for ‘select * from emp where sal>:s‘--此处可以是字符串,也就是可以动态传值的
using v_sal;--给占位符绑值
loop
fetch emp_cur into v_emp;
exit when emp_cur%notfound;
sopV(v_emp.ename);
end loop;
close emp_cur;
end;
给游标简单的做个小结
1.游标用于处理查询结果集中的数据
2.游标类型有:隐式游标、显式游标和 REF游标
3.隐式游标由 PL/SQL自动定义、打开和关闭
4.显式游标用于处理返回多行的查询
5.显式游标可以删除和更新活动集中的行
6.要处理结果集中所有记录时,可使用循环游标
7.在声明 REF游标时,不需要将 SELECT语句与其关联
*/
文章来源:http://blog.csdn.net/ysjian_pingcx/article/details/25645515接