17,086
社区成员
发帖
与我相关
我的任务
分享
--Author: phenix;Time:9:46 2009-2-10
drop table test;
create table test(id number,signvalue float);
insert into test values(1096,6.235);
insert into test values(1583,6.098);
declare
cursor c is select * from test order by id;
v_pre c%rowtype;
v_next c%rowtype;
begin
open c;
loop
fetch c into v_next;
exit when(c%notfound);
if(v_next.id=1600) then
dbms_output.put_line(v_next.id||v_next.signvalue);
else
if(v_next.id>1600)then
dbms_output.put_line(v_pre.id||v_pre.signvalue);
end if;
end if;
v_pre:=v_next;
end loop;
if(v_next<1600)then
dbms_output.put_line(v_pre.id||v_pre.signvalue);
end if;
exception
when others then
dbms_output.put_line('不存在记录');
end;
SQL> create table A(id int,signvalue float);
表已创建。
SQL> insert into A values(1,1.2);
已创建 1 行。
SQL> insert into A values(3,1.5);
已创建 1 行。
SQL> insert into A values(4,1.8);
已创建 1 行。
SQL> insert into A values(6,2.5);
已创建 1 行。
SQL> insert into A values(7,3.2);
已创建 1 行。
SQL> commit;
提交完成。
SQL> declare
2 rowa a%rowtype;
3 cursor c is select * from A;
4 v_id int:=0;
5 begin
6 open c;
7
8 loop
9 fetch c into rowa;
10 if c%found then
11
12 if rowa.id-v_id = 1 then
13 null;
14 else
15 dbms_output.put_line('得到的id值='||v_id);
16 end if;
17 v_id:= rowa.id;
18
19 else
20 exit;
21 end if;
22 end loop;
23 end;
24 /
得到的id值=1
得到的id值=4
PL/SQL 过程已成功完成。
select max(id) from tb where id < 1500 and not exists(select 1 from tb where id = 1500)