ORA:01422 Exact Fetch记录超过请求的行数,因为列变量获取所有值

问题描述 投票:0回答:1

在存储过程中,我使用列变量作为v_col1,v_col2,t_col1,t_col2。但根据业务要求,我使用了光标和集合。用于每个迭代记录的BULK COLLECT和游标的集合。但是当我声明上面的列变量并且我从tbl1存储col1的值时,来自tbl2的col2就像select col1 into v_col1 from tbl1;select col2 into v_col2 from tbl1;。我已经声明了游标,所以我想要每个记录,它将同时处理多个。我想从txt表中获取数据并将其加载到main_table表中,然后在执行此存储过程后,我获得相同数量的记录的相同记录。

create or replace procedure sp_gr
as
    cursor c1 is select * from txt%rowtype;
    v_col1 tbl1.col1%type;
    v_col2 tbl1.col2%type;
    t_col1 tbl2.col1%type;
    t_col2 tbl2.col2%type;
    type record is table of txt%rowtype;  --Staging table
    v_rc record := record();
begin
    open c1;
    loop 
        fetch c1 bulk collect into v_rc limit 1000;

        loop
            for i in 1..v_rc.count loop
         ..........
          select col1 into v_col1 from tbl1 where rownum = i;
          select col2 into v_col2 from tbl1 where rownum = i;
          select col1 into t_col1 from tbl2 where rownum = i;
          select col2 into t_col2 from tbl2 where rownum = i;

        insert when v_col1 = t_col1 then 
        into main_tbl(col1, ......)
        values (v_rc(i).col1, ......)
        when v_col1 = t_col1 AND v_col2 = t_col2
        into main_tbl(col1, ......)
        values (v_rc(i).col1, ......)
        else 
        into main_tbl(col1, ......)
        values (v_rc(i).col1, ......)
        select distinct * from txt INNER JOIN tbl1 on tbl1.col1 = txt.col1
            INNER JOIN tbl2 on tbl2.col2 = txt.col2;
        ........
     end if;
    end loop;
     exit when v_rc.count < limit;

     end loop;
     close c1;
end sp_gr;

如何解决此问题,以便列变量一次只能获得一条记录,并且第一次匹配,然后将记录从txt插入main_tbl,具有不同的值

sql oracle plsql
1个回答
0
投票
select col2 into t_col2 from tbl2 where rownum = i;

这是错的。 Rownum不是行的键/地址/存储属性。它是VIRTUAL PSEUDOCOLUMN,并且当记录已经是fetchec时分配值,从1开始总是这样。因此所有条件如rownum = 2rownum > 5都没有意义,因为总是返回FALSE并且整个SQL将不返回任何行。 Rownum只能用于限制已获取列的数量,并且可以使用:where rownum <= 10(仅选择不超过10行)或where rownum = 1(仅选择1行)。但它不能用作过滤条款。

© www.soinside.com 2019 - 2024. All rights reserved.