-----存储过程游标使用篇-----
-- 1.隐式游标 : 隐式Cursor是系统自动打开和关闭Cursor.
--SQL%ROWCOUNT 整型 代表DML语句成功执行的数据行数
--SQL%FOUND 布尔型 值为TRUE代表插入、删除、更新或单行查询操作成功
--SQL%NOTFOUND 布尔型 与SQL%FOUND属性返回值相反
--SQL%ISOPEN 布尔型 DML执行过程中为真,结束后为假
begin
update student set s_name = 'modify' where s_id = 4;
if(sql%found) then
Dbms_Output.put_line('find the rowdata 。。。');
commit;
else
Dbms_Output.put_line('unfind the rowdata 。。。');
rollback;
end if;
end;
-- 2.显示游标
declare
cursor cur is select * from student;
cursor cur2 is select * from student;
stuRow student%rowtype;
begin
for stu in cur loop
--stuRow := stu;
Dbms_Output.put_line(stu.s_name);
end loop;
open cur2;
loop
Fetch cur2 into stuRow;
Exit when cur2%notfound;
Dbms_Output.put_line(stuRow.s_name);
end loop;
Dbms_Output.put_line(cur2%ROWCOUNT);
if(cur2%isopen) then
Dbms_Output.put_line('-close cur2-');
close cur2;
end if;
end;
-- 3.动态游标
declare
type cursor_type is ref cursor;
cur cursor_type;
sqlStr varchar2(40);
rowData student%rowtype;
begin
sqlStr := 'select * from student';
open cur for sqlStr;
loop
fetch cur into rowData;
if(cur%notfound) then
exit;
Dbms_Output.put_line('-not find ...-');
end if;
Dbms_Output.put_line('-data-'||rowData.s_name);
end loop;
if(cur%isopen) then
close cur;
end if;
end;
-- 1.隐式游标 : 隐式Cursor是系统自动打开和关闭Cursor.
--SQL%ROWCOUNT 整型 代表DML语句成功执行的数据行数
--SQL%FOUND 布尔型 值为TRUE代表插入、删除、更新或单行查询操作成功
--SQL%NOTFOUND 布尔型 与SQL%FOUND属性返回值相反
--SQL%ISOPEN 布尔型 DML执行过程中为真,结束后为假
begin
update student set s_name = 'modify' where s_id = 4;
if(sql%found) then
Dbms_Output.put_line('find the rowdata 。。。');
commit;
else
Dbms_Output.put_line('unfind the rowdata 。。。');
rollback;
end if;
end;
-- 2.显示游标
declare
cursor cur is select * from student;
cursor cur2 is select * from student;
stuRow student%rowtype;
begin
for stu in cur loop
--stuRow := stu;
Dbms_Output.put_line(stu.s_name);
end loop;
open cur2;
loop
Fetch cur2 into stuRow;
Exit when cur2%notfound;
Dbms_Output.put_line(stuRow.s_name);
end loop;
Dbms_Output.put_line(cur2%ROWCOUNT);
if(cur2%isopen) then
Dbms_Output.put_line('-close cur2-');
close cur2;
end if;
end;
-- 3.动态游标
declare
type cursor_type is ref cursor;
cur cursor_type;
sqlStr varchar2(40);
rowData student%rowtype;
begin
sqlStr := 'select * from student';
open cur for sqlStr;
loop
fetch cur into rowData;
if(cur%notfound) then
exit;
Dbms_Output.put_line('-not find ...-');
end if;
Dbms_Output.put_line('-data-'||rowData.s_name);
end loop;
if(cur%isopen) then
close cur;
end if;
end;