日期:2014-05-17  浏览次数:22128 次

嵌套声明游标出错,ORA-01400
嵌套声明了两个游标:
SQL code
DECLARE
   v_acc_month date;
   CURSOR c1
   IS
      SELECT    b.part_no
             || '  ,  '
             || TO_CHAR (material.get_minor_wh_stock_qty (b.part_no, c.warehouse))
                                                                  debit_item
        FROM material.minor_material b, public_mgr.warehouse c
       WHERE c.material_flag = 'Y'
         AND c.cost_flag = 'Y'
         AND material.get_minor_wh_stock_qty (b.part_no, c.warehouse) < 0;
BEGIN
      FOR c1_rec IN c1
      LOOP
         :text1 := :text1 || c1_rec.debit_item || CHR (10) || CHR (13);
      END LOOP;

      DECLARE
         vcount       NUMBER       := 1;
         vserial_no   VARCHAR2 (4);

         CURSOR c1
         IS
            ...
      BEGIN
         FOR c1_rec IN c1
         LOOP
            INSERT INTO material.minor_materialiohis
                        (part_no, cday, CATEGORY,
                         iotype, qty, internal_sheetno,
                         dept, part_name,
                         serial_no, unit,
                         deliveryed, origin_qty,
                         stock_position, batch_no
                        )
                 VALUES (c1_rec.part_no, c1_rec.cday, c1_rec.CATEGORY,
                         c1_rec.iotype, c1_rec.qty, c1_rec.internal_sheetno,
                         c1_rec.dept, c1_rec.part_name,
                         TO_CHAR (vcount, '099'), c1_rec.unit,
                         c1_rec.deliveryed, c1_rec.origin_qty,
                         c1_rec.stock_position, c1_rec.batch_no
                        );

            vcount := vcount + 1;
         END LOOP;
      END;
END;



不知道两个游标会不会混淆?因为定义了相同的名字

游标查询SQL全部有数据,插入时为什么会报:无法插入NULL值错误?

------解决方案--------------------
引用楼主 BrainStorm_Lee 的帖子:
嵌套声明了两个游标: 
 INSERT INTO material.minor_materialiohis
(part_no, cday, CATEGORY,
iotype, qty, internal_sheetno,
dept, part_name,
serial_no, unit,
deliveryed, origin_qty,
stock_position, batch_no
)
VALUES (c1_rec.part_no, c1_rec.cday, c1_rec.CATEGORY,
c1_rec.iotype, c1_rec.qty, c1_rec.internal_sheetno,
c1_rec.dept, c1_rec.part_name,
TO_CHAR (vcount, '099'), c1_rec.unit,
c1_rec.deliveryed, c1_rec.origin_qty,
c1_rec.stock_position, c1_rec.batch_no
);


------解决方案--------------------
不会混淆,begin... end已经限制了使用范围,和局部变量的含义一样
For example, if you enter:

connect scott/tiger create table a (a1 number not null); insert into a values (null); 
Oracle returns:

ORA-01400 cannot insert NULL into ("SCOTT"."A"."A1") : which means you cannot insert NULL into "SCOTT"."A"."A1".

------解决方案--------------------
看不大懂,知道你不轻松,头疼了....我还烦你,sorry( ⊙ o ⊙ )啊!帮顶....~~~