那么我们使用Oracle游标
游标分为:静态游标和引用游标(动态游标)
静态游标:由用户定义(隐式游标、显示游标)结果集不变
引用游标游标:结果集变化
隐式游标:用DML操作时,自动使用隐式游标。我们可以使用隐式游标判断SQL语句执行结果
自动声明和处理的。在Session会话区,开启游标。处理后自动关闭。可以返回单行查询。
隐式游标使用:
declare
%NOTFOUND -- 执行行没有找到。
%FOUND --执行行找到
%ROWCOUNT --游标影响行数
%ISOPEN -- 当前游标是否打开
我们现在通过游标来看看上篇文章的例子
通过循环来遍历数据:
1、loop when循环
declare
cursor myCur is select * from hr.jobs;
oneRow hr.jobs%rowtype;
begin
open myCur;
loop
fetch myCur into oneRow;
dbms_output.put_line(oneRow.job_id ||' ' ||onerow.job_title);
exit when myCur%notFound;
end loop;
close myCur;
end;
2、while 循环
declare
cursor myCur is select * from hr.jobs;
oneRow hr.jobs%rowtype;
begin
open myCur;
fetch myCur into oneRow;
while (myCur%found)
loop
dbms_output.put_line(oneRow.job_id ||' ' ||onerow.job_title);
fetch myCur into oneRow;
end loop;
close myCur;
end;
3、for 循环
declare
cursor myCur is select * from hr.jobs;
oneRow hr.jobs%rowtype;
begin
for oneRow in myCur loop
dbms_output.put_line(oneRow.job_id ||' ' ||onerow.job_title);
end loop;
end;
结果如下:
AD_PRES President
AD_VP Administration Vice President
AD_ASST Administration Assistant
FI_MGR Finance Manager
FI_ACCOUNT Accountant
AC_MGR Accounting Manager
AC_ACCOUNT Public Accountant
SA_MAN Sales Manager
SA_REP Sales Representative
PU_MAN Purchasing Manager
PU_CLERK Purchasing Clerk
ST_MAN Stock Manager
ST_CLERK Stock Clerk
SH_CLERK Shipping Clerk
IT_PROG Programmer
MK_MAN Marketing Manager
MK_REP Marketing Representative
HR_REP Human Resources Representative
PR_REP Public Relations Representative
分享到:
相关推荐
sql游标遍历实例
在sql中,通过id查询出一个字段的值ordernumber,利用游标循环遍历对应字段的值ordernumber1,(比较ordernumber是否存在,存在就在后+'_1',+'_2'...)
oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结oracle游标的总结
CN110543495A-游标遍历存储方法及装置.pdf
ORACLE 游标使用示例,给大家可以共享一下。
Oracle游标的一般使用
oracle游标使用大全,详细描述了如何使用游标,创建游标等等
游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。
Oracle游标使用方法及语法大全.doc
oracle 游标FOR循环
详细的oracle游标用法,简单易懂,是学习游标的好材料
oracle游标优化以及oracle游标的作用和游标的概念。
Oracle游标使用方法及语法大全
Oracle 游标! 值得下载看看!资源免费,大家分享!!
declare cur cursor for select AccountID,EmployeeName from #Temp open cur fetch next from cur into @tmpID, @tmpName fetch next from cur into @tmpID, @tmpName end close cur ...
oracle游标 详解 精析 示例 真正能把游标讲透、说全、调理清晰的讲义。 游标犹如C语言的指针:灵活、实用、高效。 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。 游标是一个通过定义...
详细介绍了 oracle的游标使用 及 实例
oracle游标使用大全,其中有很多例子,有助于大家理解,希望对初学者有帮助