bufegar 2008-10-10 17:24
Oracle游标学习笔记及在java中的调用(转帖)
游标按以下操作进行
parse 解析
bind 绑定
open 打开
execute 执行
fetch 回取
close 关闭
1.写自己第一个游标PL/SQL
declare
cursor c_s is select * from user_tables;
begin
open c_s; --打开游标
close c_s;--关闭游标
end;
游标的4个属性 %found,%notfound,%rowcount,%isopen
1.%found 游标有记录则返回true否则false
declare
cursor c_s is select * from user_tables;
cc c_s%rowtype;
begin
open c_s; --打开游标
fetch c_s into cc;
while c_s%found loop
fetch c_s into cc;
end loop;
close c_s;--关闭游标
end;
2.%notfound 游标没记录则返回true否则false(个人感觉有点多余)
declare
cursor c_s is select * from user_tables;
cc c_s%rowtype;//游标变量
begin
open c_s; --打开游标
fetch c_s into cc;
while c_s%found loop
exit when c_s%notfound;
end loop;
close c_s;--关闭游标
end;
3.%rowcount 返回游标取回的记录数目
declare
cursor c_s is select * from user_tables;
cc c_s%rowtype;
begin
open c_s; --打开游标
fetch c_s into cc;
while c_s%found loop
dbms_output.put_line(c_s%rowcount);
end loop;
close c_s;--关闭游标
end;
4.%isopen 如果游标打开就返回true 否则false
declare
cursor c_s is select * from user_tables;
begin
if c_s%isopen then
dbms_output.put_line('cursor is open');
else
open c_s; --打开游标
end if;
close c_s;--关闭游标
end;
游标参数
declare
cursor c_s(cs_id number) is select * from admin id=cs_id;
begin
open c_s(10); --打开带参数的游标
close c_s;--关闭游标
end;
游标中的for update
declare
cursor c_s is select id from admin
for update of id //查询时锁定 id列
begin
open c_s;
commit;//提交释放锁 或者可以用 rollback
close c_s;
end;
游标中的where cursor of
UPDATE table_name SET set_clause WHERE CURSOR OF cursor_name; //更新游标所指向的那条记录
DELETE FROM table_name WHERE CURSOR OF cursor_name; //删除游标所指向的那条记录
游标中的ref cursor类型
TYPE c_s IS REF CURSOR RETRUN table%rowtype; //这种形式为强类型在声明的时候就定了为行类型
TYPE c_s IS REF CURSOR;//弱类型 不与记录的数据类型关联
例子:
declare
TYPE c_s IS REF CURSOR RETRUN table%rowtype;
TYPE c_s2 IS REF CURSOR;
var_cs c_s;//声明为游标变量的类型
begin
OPEN c_s FOR select * from admin;
CLOSE c_s;
end;
在java中调用以游标为返回值的存储过程时,先要import oracle.jdbc.OracleTypes,然后把返回值注册为OracleTypes.Cursor,再通过调用getCursor返回游标的类型。
出处:[url]http://blog.sina.com.cn/s/blog_4cf3bd2501000aeh.html[/url]
蝶恋meng 2008-10-13 09:43
:lollllll 谢谢分享。
[img]http://p8.images22.51img1.com/6000/qwb654/852fad10dd443bb0292fff0ca514f90d.gif[/img]
[url=http://www.k8zw.cn/files/article/html/0/208/]盘龙[/url] [url=http://www.k8zw.cn/files/article/html/0/280/]琴帝[/url]