- 1、本文档共9页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Oracle存储过程学习游标CURSOR使用
游标CURSOR的使用学习
游标的类型:
1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql。
2,显式游标:显式游标用于处理返回多行的查询。
3,REF 游标:REF 游标用于处理运行时才能确定的动态 SQL 查询的结果
一、隐式游标
在PL/SQL中使用DML语句时自动创建隐式游标 q隐式游标自动声明、打开和关闭,其名为 SQL q通过检查隐式游标的属性可以获得最近执行的DML 语句的信息
q隐式游标的属性有:
q%FOUND – SQL 语句影响了一行或多行时为 TRUE
q%NOTFOUND – SQL 语句没有影响任何行时为TRUE
q%ROWCOUNT – SQL 语句影响的行数
q%ISOPEN - 游标是否打开,始终为FALSE
在select中有两个中比较常见的异常: 1. NO_DATA_FOUND 2. TOO_MANY_ROWS
declare
sname1 student.sname%TYPE;
begin
select sname into sname1 from student;
if sql%found then
dbms_output.put_line(sql%rowcount);
else
dbms_output.put_line(没有找到数据);
end if;
exception
when too_many_rows then
dbms_output.put_line(查找的行记录多于1行);
when no_data_found then
dbms_output.put_line(未找到匹配的行);
end;
显式游标:
sqlserver与oracle的不同之处在于: 最后sqlserver会deallocate 丢弃游标,而oracle只有前面四步: 声明游标、打开游标、使用游标读取记录、关闭游标。显式游标的使用:
无参数游标
declare
sname varchar2(20); --声明变量
cursor student_cursor is
select sname from student; --声明游标
begin
open student_cursor; --打开游标
fetch student_cursor
into sname; --让游标指针往下移动
while student_cursor%found --判断游标指针是否指向某行记录
loop
--遍历
dbms_output.put_line(学生姓名 || sname);
fetch student_cursor
into sname;
end loop;
close student_cursor;
end;
有参数游标
declare
sname student.sname%type;
sno student.sno%type;
cursor student_cursor(input_sno number) is
select s.sname, s.sno from student s where s.sno input_sno; --声明带参数的游标
begin
sno := 请输入学号; --要求从客户端输入参数值,相当于占位符;
open student_cursor(sno); --打开游标,并且传递参数
fetch student_cursor
into sname, sno; --移动游标
while student_cursor% found loop
dbms_output.put_line(学号为: || sno || 姓名为: || sname);
fetch student_cursor
into sname, sno;
end loop;
close student_cursor;
end;
循环游标
-- Created on 18-1月-15 by 永文
declare
stu1 student%rowtype; --这里也不需要定义变量来接收fetch到的值
cursor student_cursor is
select * from student;
begin
open student_cursor; --这里不需要开启游标
文档评论(0)