游标
来源:互联网 发布:最新伪基站设备 淘宝 编辑:程序博客网 时间:2024/06/06 13:11
--显示游标
declare
cursor cur_stu is select sname,sadd from students where sid=5 ;--定义一个游标
row_stu cur_stu%rowtype;--基于游标定义一个记录
begin
open cur_stu;--打开游标
loop
fetch cur_stu into row_stu;--从游标中提取一行到记录行中
exit when cur_stu%notfound;--退出条件
if cur_stu%found then
dbms_output.put_line(row_stu.sname||row_stu.sadd);
end if;
end loop;
close cur_stu ;--关闭游标
end;
等同于
declare
s_name students.sname%type;
s_add students.sadd%type;
cursor cur_stu is select sname,sadd from students where sid=5 ;--定义一个游标
row_stu cur_stu%rowtype;--基于游标定义一个记录
begin
open cur_stu;--打开游标
loop
fetch cur_stu into s_name ,s_add ;--从游标中提取一行到记录行中
exit when cur_stu%notfound;--退出条件
if cur_stu%found then
dbms_output.put_line(s_name ||s_add);
end if;
end loop;
close cur_stu ;--关闭游标
end;
--ref 游标 (引用游标)
declare
type stu_cursor_type is ref cursor;--创建一个引用游标
stu_cursor stu_cursor_type;--定义游标变量
stu_record students%rowtype;--基于游标定义一个记录
begin
open stu_cursor for select * from students;--打开游标变量
loop
fetch stu_cursor into stu_record;----从游标中提取一行到记录行中
exit when stu_cursor%notfound;--退出条件
if stu_cursor%found then
dbms_output.put_line(stu_record.sname||stu_record.sadd);
end if;
end loop;
close stu_cursor;--关闭游标
end;
-----动态绑定(1 使用execute immediate '' using 参数列表;
2 使用open 游标名 for '' using 参数列表;
)
1 使用execute immediate '' using 参数列表;
declare
begin
execute immediate
'update students set smoney=999 where sid=:no' using 5;
end;
2 使用open 游标名 for '' using 参数列表;
declare
type stu_cursor_type is ref cursor;
stu_cursor stu_cursor_type;
stu_table students%rowtype;
begin
open stu_cursor for 'select * from students where sid=:no' using 5;
loop
fetch stu_cursor into stu_table;
exit when stu_cursor%notfound;
dbms_output.put_line(stu_table.sname);
end loop;
end;
declare
cursor cur_stu is select sname,sadd from students where sid=5 ;--定义一个游标
row_stu cur_stu%rowtype;--基于游标定义一个记录
begin
open cur_stu;--打开游标
loop
fetch cur_stu into row_stu;--从游标中提取一行到记录行中
exit when cur_stu%notfound;--退出条件
if cur_stu%found then
dbms_output.put_line(row_stu.sname||row_stu.sadd);
end if;
end loop;
close cur_stu ;--关闭游标
end;
等同于
declare
s_name students.sname%type;
s_add students.sadd%type;
cursor cur_stu is select sname,sadd from students where sid=5 ;--定义一个游标
row_stu cur_stu%rowtype;--基于游标定义一个记录
begin
open cur_stu;--打开游标
loop
fetch cur_stu into s_name ,s_add ;--从游标中提取一行到记录行中
exit when cur_stu%notfound;--退出条件
if cur_stu%found then
dbms_output.put_line(s_name ||s_add);
end if;
end loop;
close cur_stu ;--关闭游标
end;
--ref 游标 (引用游标)
declare
type stu_cursor_type is ref cursor;--创建一个引用游标
stu_cursor stu_cursor_type;--定义游标变量
stu_record students%rowtype;--基于游标定义一个记录
begin
open stu_cursor for select * from students;--打开游标变量
loop
fetch stu_cursor into stu_record;----从游标中提取一行到记录行中
exit when stu_cursor%notfound;--退出条件
if stu_cursor%found then
dbms_output.put_line(stu_record.sname||stu_record.sadd);
end if;
end loop;
close stu_cursor;--关闭游标
end;
-----动态绑定(1 使用execute immediate '' using 参数列表;
2 使用open 游标名 for '' using 参数列表;
)
1 使用execute immediate '' using 参数列表;
declare
begin
execute immediate
'update students set smoney=999 where sid=:no' using 5;
end;
2 使用open 游标名 for '' using 参数列表;
declare
type stu_cursor_type is ref cursor;
stu_cursor stu_cursor_type;
stu_table students%rowtype;
begin
open stu_cursor for 'select * from students where sid=:no' using 5;
loop
fetch stu_cursor into stu_table;
exit when stu_cursor%notfound;
dbms_output.put_line(stu_table.sname);
end loop;
end;
0 0
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 游标
- 程序员的十层楼
- 挑战面试编程:最大连续子序列和
- oracle NVL、NVL2,、NULLIF、COALESCE函数的用法
- 树型结构1 堆排序 线索二叉树 huffman
- JS弹窗效果的运用(代码)
- 游标
- 自增长序列
- Java代码检查和bug分析工具
- HDOJ 3790 最短路径问题
- The source code of the Account class of the famous IM app
- 程序员怎么才能提高代码编写速度?
- Hibernate二级缓存的配置
- 内存和指针泄露常见问题
- Coursera公开课Functional Programming Principles in Scala习题解答:Week 1