plsql代码出错 - ORA 06550

时间:2014-07-22 08:44:33

标签: sql oracle cursor

DECLARE  
   CURSOR cur  
   IS  
      SELECT   dm.fash unit, h1.groupin, '01' startin, SUM (h1.weight)  
          FROM tab1 t1 INNER JOIN tab2 h1 ON t1.ALTER = h1.unit  
               INNER JOIN unit dm ON t1.col1 = dm.unit  
         WHERE t1.col1 IN (SELECT col1  
                             FROM tab1  
                            WHERE sw = 1)  
           AND h1.eve = 'Al'  
           AND NOT EXISTS (SELECT 1  
                             FROM tab2  
                            WHERE unit = t1.col1)  
      GROUP BY dm.fash, h1.groupin;  


BEGIN  
   FOR rec_in IN cur  
   LOOP  
      INSERT      /*+append*/INTO targ  
                  (unit, groupin, startin,  
                   weight, length, height  
                  )  
           VALUES (rec_in.unit, rec_c1.groupin, rec_c1.startin,  
                   rec_c1.weight, 4, 5  
                  );  
   END LOOP;  

   COMMIT;  
END;  
/

这可能是一件容易的事。但我无法找到原因。

当我运行上述查询时,我收到错误消息,说明组件“重量”'必须声明(值字段rec_c1.weight中的字段权重) 表格Targ有列 - UNIT,GROUPIN,STARTIN,WEIGHT,LENGTH,HEIGHT

2 个答案:

答案 0 :(得分:0)

您的游标循环中有一个拼写错误,它限定了游标中的列。您已将循环标识符指定为rec_in,但已为groupin,startin和weight输入了rec_c1:

BEGIN  
FOR rec_in IN cur  
LOOP  
   INSERT      /*+append*/INTO targ  
               (unit, groupin, startin,  
                weight, length, height  
               )  
        VALUES (rec_in.unit, rec_c1.groupin, rec_c1.startin,  
                rec_c1.weight, 4, 5  
               );  
END LOOP;  

COMMIT;  
END;  

/

这必须与单位一样rec_in:

BEGIN  
FOR rec_in IN cur  
LOOP  
   INSERT      /*+append*/INTO targ  
               (unit, groupin, startin,  
                weight, length, height  
               )  
        VALUES (rec_in.unit, rec_in.groupin, rec_in.startin,  
                rec_in.weight, 4, 5  
               );  
END LOOP;  

COMMIT;  
END;  

/

答案 1 :(得分:0)

我不了解Oracle,但我想你需要命名SUM列,即 SUM (h1.weight) weight

但你真的需要光标吗?你不能用select?

进行插入
INSERT INTO targ  
                  (unit, groupin, startin,  
                   weight, length, height  
                  )
SELECT   dm.fash unit, h1.groupin, '01' startin, SUM (h1.weight)  
          FROM tab1 t1 INNER JOIN tab2 h1 ON t1.ALTER = h1.unit  
               INNER JOIN unit dm ON t1.col1 = dm.unit  
         WHERE t1.col1 IN (SELECT col1  
                             FROM tab1  
                            WHERE sw = 1)  
           AND h1.eve = 'Al'  
           AND NOT EXISTS (SELECT 1  
                             FROM tab2  
                            WHERE unit = t1.col1)  
      GROUP BY dm.fash, h1.groupin;