oracle 游标例子不同的 打开方式 -判断最后一条记录
2012-03-14 16:14
936 查看
--1 显示游标
declare
cursor v_sc1 is
select sendnum,registedate from user_info t for update of t.userlevel,nicheng nowait; --of 后面可以多个变量
n_level number;
n_sendnum number;
d_registedate date;
begin
n_level:=0;
open v_sc1;
loop
fetch v_sc1 into n_sendnum, d_registedate,n_id;
exit when v_sc1%notfound; --一定要有
if (n_sendnum>0) and (n_sendnum<=100) and (d_registedate<add_months(sysdate,-3))then
n_level :=1;
else
n_level :=3;
end if;
update user_info set userlevel = n_level where current of v_sc1; --更新数据 只更新当前数据;不用再次执行全表扫描
end loop;
end;
/
--2 带参数的游标
declare
cursor c_test(pid_1 number,pid_2 number) is select * from t1 where t1.emp_no> pid_1 and t1.emp_no<pid_2 for update of emp_no;
i number:=0;
begin
for i in 100..102 loop --外循环 :在查询数据量大的表时一次性查出风险太大。可以分批执行
for v_test in c_test(i,i+100) loop
update t1 set t1.tel='13838100400' where current of c_test;
end loop;
commit;
end loop;
end;
--3 另一种打开方式
declare
type refcursor is ref cursor;
v_cursor refcursor;
begin
open cur for select t1.tel from t1;
loop
exit when cur%notfound;
fetch cur into v_tel ;
end loop;
end;
4- 判断游标指向最后一条记录
loop
i:=i+1;
if i=t_cursor%rowcount-1 then
最后一条记录
end if;
declare
cursor v_sc1 is
select sendnum,registedate from user_info t for update of t.userlevel,nicheng nowait; --of 后面可以多个变量
n_level number;
n_sendnum number;
d_registedate date;
begin
n_level:=0;
open v_sc1;
loop
fetch v_sc1 into n_sendnum, d_registedate,n_id;
exit when v_sc1%notfound; --一定要有
if (n_sendnum>0) and (n_sendnum<=100) and (d_registedate<add_months(sysdate,-3))then
n_level :=1;
else
n_level :=3;
end if;
update user_info set userlevel = n_level where current of v_sc1; --更新数据 只更新当前数据;不用再次执行全表扫描
end loop;
end;
/
--2 带参数的游标
declare
cursor c_test(pid_1 number,pid_2 number) is select * from t1 where t1.emp_no> pid_1 and t1.emp_no<pid_2 for update of emp_no;
i number:=0;
begin
for i in 100..102 loop --外循环 :在查询数据量大的表时一次性查出风险太大。可以分批执行
for v_test in c_test(i,i+100) loop
update t1 set t1.tel='13838100400' where current of c_test;
end loop;
commit;
end loop;
end;
--3 另一种打开方式
declare
type refcursor is ref cursor;
v_cursor refcursor;
begin
open cur for select t1.tel from t1;
loop
exit when cur%notfound;
fetch cur into v_tel ;
end loop;
end;
4- 判断游标指向最后一条记录
loop
i:=i+1;
if i=t_cursor%rowcount-1 then
最后一条记录
end if;
相关文章推荐
- 判断当前页是否为最后一页,且该页只有最后一条记录
- smarty 里面如何判断 是最后一条记录 就不输出 “|”
- oracle开发中的存储过程,游标,if判断经典例子
- Oracle 、sql如何查询表的第一条记录和最后一条记录
- oracle 记录每一条数据最后更新的时间(ora_rowscn)
- oracle 查询最后一条记录
- thinkphp中volist if条件判断最后一条记录
- jsp <c:forEach> 判断第一条 或 最后一条记录
- ThinkPHP模板中判断volist循环的最后一条记录的验证方法
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- 使用游标的方式删除表中相同的记录,并只留下其中一条
- ThinkPHP模板中判断volist循环的最后一条记录
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- oracle中得到最后一条记录
- ORACLE中SQL取最后一条记录的几种方法
- Oracle分析函数系列之first_value/last_value:在记录集中查找第一条记录和最后一条记录
- oracle开发中的存储过程,游标,if判断经典例子
- ASP判断记录集到达最后一条记录
- oracle分析函数系列之first_value/last_value:在记录集中查找第一条记录和最后一条记录
- ThinkPHP模板中判断volist循环的最后一条记录的验证方法