惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
D
Docker
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
V
V2EX

博客园 - karlen

Oracle SQL 性能优化技巧 Ajax應用實例 轉載:分隔數組返回table 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 Baby 给老爸的留言 轉載:利用SQL*Loader将 Excel 数据导出到数据库中 Oracle 10g (10.2.0.2) for NT 企业版 轉載:oracle基礎知識學習二 轉載:oracle基礎知識學習一 Oracle 9i SP實例 Sql2005安裝手冊 Ajax應用實例 简单的生活目标 工作中的小虚惊 心情随感 一起努力 table manners 项目管理中的好管家-CMMI过程中的PPQA工作 Open your mouth, just speaking!
游标遍历%rowtype中的记录
karlen · 2008-12-15 · via 博客园 - karlen

如何遍历所有记录,不用每次输入特定的值去查询。那么我们使用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