光标内的光标,用于比较未执行的同一列的记录

时间:2017-03-03 07:23:44

标签: plsql cursor

我正在研究小型PL / SQL程序,它使用游标将列记录与自身进行比较。当我执行代码时,需要很长时间才会陷入无限循环。

Table Data :-

STD
----
 2
 1
 4
 3
 6
 5
 8
 7
 10
 9
 12
 11

下面是我的代码: -

declare 
s number;
s1 number;
c number := 0;
cursor c1 is (select std from data);
cursor c2 is (select std from data);
begin
open c1;
loop
    fetch c1 into s;
     open c2;
        loop
            fetch c2 into s1;
            if s < s1
            then    
                c := c + 1;
           end if;
        end loop;
     insert into con values(c);
     close c2;       
end loop;
close c1; 
end;
/

预期结果: -

C=10
C=10
C=8
C=8
C=6
C=6
C=4
C=4
C=2
C=2
C=0
C=null

2 个答案:

答案 0 :(得分:0)

你应该添加

EXIT WHEN c1%NOTFOUND;

EXIT WHEN c2%NOTFOUND;

相应的提取后。 e.g。

declare 
s number;
s1 number;
c number := 0;
cursor c1 is (select std from data);
cursor c2 is (select std from data);
begin
open c1;
loop
    fetch c1 into s;
    EXIT WHEN c1%NOTFOUND;
     open c2;
        loop
            fetch c2 into s1;
            EXIT WHEN c2%NOTFOUND;
            if s < s1
            then    
                c := c + 1;
           end if;
        end loop;
     insert into con values(c);
     close c2;       
end loop;
close c1; 
end;
/

答案 1 :(得分:0)

如果我理解得很清楚,那么您假设您的数据按照您发布的方式进行了排序;在此情况下,您需要为数据集中的每个值计算大于当前值并出现在当前行之后的值的数量。

假设我们可以使用附加列(ID)来考虑您的数据样本以提供订单:

create table data(ID, std) as (
    select  1,  2 from dual union all
    select  2,  1 from dual union all
    select  3,  4 from dual union all
    select  4,  3 from dual union all
    select  5,  6 from dual union all
    select  6,  5 from dual union all
    select  7,  8 from dual union all
    select  8,  7 from dual union all
    select  9, 10 from dual union all
    select 10,  9 from dual union all
    select 11, 12 from dual union all
    select 12, 11 from dual
)

如果我理解你的需要,你可以避免PLSQL和光标,并通过一个查询得到结果:

select d1.std, count(d2.id)
from data d1
       left outer join data d2
         on ( d1.ID < d2.ID and d2.std > d1.std)
group by d1.std, d1.id   
order by d1.ID  

在样本数据上,这给出了:

   STD     COUNT(D2.ID)
---------- ------------
     2           10
     1           10
     4            8
     3            8
     6            6
     5            6
     8            4
     7            4
    10            2
     9            2
    12            0
    11            0