--游标(fetch在循环外面)
declare cursor cu_pro is
select id,pname,price from product;
pid product.id%type;
pname product.pname%type;
price product.price%type;
begin
open cu_pro;
fetch cu_pro into pid,pname,price;
while cu_pro%found loop
dbms_output.put_line('编号:'||pid||',名称:'||pname||',价格:'||price);
fetch cu_pro into pid,pname,price;
end loop;
close cu_pro;
end;
--静态游标(fetch在循环里面,用loop包裹)
declare cursor cu_product is
select id,pname,price from product;
pid product.id%type;
pname product.pname%type;
price product.price%type;
begin
open cu_product;
loop
fetch cu_product into pid,pname,price;
exit when cu_product%notfound;--exit必须存在于exit中
dbms_output.put_line('编号:'||pid||',名称:'||pname||',价格:'||price);
end loop;
close cu_product;
end;
--静态游标(使用行变量)
declare cursor cu_product is
select * from product;
pro product%rowtype;
begin
open cu_product;
loop
fetch cu_product into pro;
exit when cu_product%notfound;
dbms_output.put_line('编号:'||pro.id||',名称:'||pro.pname||',价格:'||pro.price);
end loop;
close cu_product;
end;
--动态游标(强类型)
begin
declare
type pro_cu is ref cursor
return product%rowtype;--返回类型(强制类型)
e_count number;
product_cu pro_cu;--游标变量
products product%rowtype;--行变量
begin
select count(*) into e_count from product where pname='ronaldo';
case
when e_count>0 then
open product_cu for select * from product where pname='ronaldo'; --游标打开指定的sql查询
when e_count>2 then
open product_cu for select * from product;
else
open product_cu for select * from product;
end case;
loop
fetch product_cu into products;
exit when product_cu%notfound;
dbms_output.put_line('编号:'||products.id||',名称:'||products.pname||',价格:'||products.price);
end loop;
end;
end;
--动态游标(弱类型):弱类型不需要指定返回的类型
begin
declare
type pro_cus is ref cursor;
e_count number;
emps emp%rowtype;
products product%rowtype;
pro_cu pro_cus;
begin
select count(*) into e_count from product where pname='ronaldo';
case
when e_count>0 then
open pro_cu for select * from product where pname='ronaldo';
loop
fetch pro_cu into products;
exit when pro_cu%notfound;
dbms_output.put_line('编号:'||products.id||',名称:'||products.pname||',价格:'||products.price);
end loop;
else
open pro_cu for select * from emp;
loop
fetch pro_cu into emps;
exit when pro_cu%notfound;
dbms_output.put_line('姓名:'||emps.ename||'工作:'||emps.job);
end loop;
end case;
end;
end;
分享到:
相关推荐
oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结
Oracle游标的一般使用
oracle游标使用大全,详细描述了如何使用游标,创建游标等等
ORACLE 游标使用示例,给大家可以共享一下。
Oracle游标使用方法及语法大全.doc
详细的oracle游标用法,简单易懂,是学习游标的好材料
oracle 游标FOR循环
Oracle游标使用方法及语法大全
oracle游标 详解 精析 示例 真正能把游标讲透、说全、调理清晰的讲义。 游标犹如C语言的指针:灵活、实用、高效。 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。 游标是一个通过定义...
oracle游标优化以及oracle游标的作用和游标的概念。
oracle游标使用大全,其中有很多例子,有助于大家理解,希望对初学者有帮助
详细介绍了 oracle的游标使用 及 实例
Oracle 游标! 值得下载看看!资源免费,大家分享!!
关于游标使用问题的练习附答案。游标分隐式和显示游标;动态和ref游标。。。
游标是存储过程最基本的要会的,用于访问 ORACLE 数据库的 DDL 和 TCL 语
主要描述oracle中游标的学习隐式游标和显示游标
该资源系本人培训期间的关于ORCL 游标所有实用案例,在此共享希望对大家学习有所帮助
我自己编写的oracle游标案例,希望对大家有点帮助!