Oracle INSERT ... SELECT插入0行,但select确实返回超过0行

时间:2015-03-08 23:41:13

标签: oracle select insert

我有一个Insert ... Select语句,用于在PL / SQL过程中将某些行插入表中。在语句结束时,我使用SQL%ROWCOUNT来捕获已插入的行数,并将其放入日志表中。

我在这里有一个案例,其中日志显示已经插入了0行,并且我已经在目标表中验证了没有插入任何内容,但是当我运行select语句时,它确实返回了超过0行。

这是代码,

INSERT INTO J2_TRACK_PRICES 

(pgr_price_group_no,pgr_dep_dept_no,
                         track_no,
                         track_colour,
                         cost_price,
                         retail_price,
                        selling_price,
                          buying_price,
                         ticket_retail_price,
                                     ticket_selling_price,
                                     cost_gst_rate,
                                     retail_gst_rate)

           SELECT DISTINCT
                  pgr.price_group_no,
                  pgr.dep_dept_no,
                  bpd.track_no,
                  bpd.track_colour,
                  bpd.cost_price,
                  bpd.cost_price,
                  bpd.retail_price,
                  bpd.retail_price

FROM B2_TMP_PURCHASE_ORDER_DET bpd, J2_PRICE_GROUPS pgr

            WHERE     bpd.bph_po_no = dbt_po_no
                  AND pgr.dep_dept_no = dbt_dept_no
                  AND bpd.line_no IN (SELECT MIN (bpd2.line_no)
                                        FROM B2_TMP_PURCHASE_ORDER_DET bpd2
                                       WHERE bpd2.bph_po_no = bpd.bph_po_no
                                      GROUP BY bpd2.track_no, bpd2.track_colour)
                  AND NOT EXISTS
                         (SELECT 1
                            FROM J2_TRACK_PRICES trp
                           WHERE     trp.pgr_price_group_no = pgr.price_group_no
                                 AND trp.pgr_dep_dept_no = pgr.dep_dept_no
                                 AND trp.track_no = bpd.track_no
                                 AND trp.track_colour = bpd.track_colour);

你们之前有没有遇到过这个?

非常感谢

1 个答案:

答案 0 :(得分:0)

这是程序。感谢

PROCEDURE create_track_prices (dbt_po_no IN J2_PURCHASE_ORDER_HDR.po_no%TYPE,
                           dbt_dept_no IN j2_departments.dept_no%TYPE,
                           dbt_tracking_level IN J2_SKU_MASTER.tracking_level%TYPE)

IS
  n_true    CONSTANT INTEGER := 0;
  n_false   CONSTANT INTEGER := 1;
  n_rc               INTEGER;
  n_pre_gst          INTEGER;
  n_rate_none        J2_TRACK_PRICES.cost_gst_rate%TYPE;
  /*
   * vars for monitoring purpose TPR916
   */
  mon_grp            NUMBER;
  mon_boa_det        NUMBER;
  mon_cls            NUMBER;
BEGIN

  IF dbt_tracking_level = J2_Constants_Pkg.class_level
  THEN
     mon_catch_boa ('L5', dbt_po_no, 'tracking level: ' || dbt_tracking_level);

     SELECT COUNT (*)
       INTO mon_grp
       FROM J2_PRICE_GROUPS
      WHERE dep_dept_no = dbt_dept_no;

     mon_catch_boa ('L6', dbt_po_no, mon_grp || ' groups found');

     SELECT COUNT (*)
       INTO mon_boa_det
       FROM B2_TMP_PURCHASE_ORDER_DET
      WHERE bph_po_no = dbt_po_no;

     mon_catch_boa ('L7', dbt_po_no, mon_boa_det || ' detail records found');

     SELECT COUNT (*)
       INTO mon_cls
       FROM J2_CLASSES
      WHERE dep_dept_no = dbt_dept_no;

     mon_catch_boa ('L8', dbt_po_no, mon_cls || ' classes found');

     BEGIN
        INSERT INTO J2_TRACK_PRICES (pgr_price_group_no,
                                     pgr_dep_dept_no,
                                     track_no,
                                     track_colour,
                                     cost_price,
                                     ticket_retail_price,
                                     ticket_selling_price,
                                     buying_price,
                                     retail_price,
                                     selling_price,
                                     cost_gst_rate,
                                     retail_gst_rate)
           SELECT DISTINCT pgr.price_group_no,
                           pgr.dep_dept_no,
                           bpd.track_no,
                           J2_Constants_Pkg.default_track_colour,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           cla.cost_gst_rate,
                           cla.retail_gst_rate
             FROM B2_TMP_PURCHASE_ORDER_DET bpd, J2_PRICE_GROUPS pgr, J2_CLASSES cla
            WHERE     bpd.bph_po_no = dbt_po_no
                  AND pgr.dep_dept_no = dbt_dept_no
                  AND bpd.line_no IN (SELECT MIN (bpd2.line_no)
                                        FROM B2_TMP_PURCHASE_ORDER_DET bpd2
                                       WHERE bpd2.bph_po_no = bpd.bph_po_no
                                      GROUP BY bpd2.track_no, bpd2.track_colour)
                  AND NOT EXISTS
                         (SELECT 1
                            FROM J2_TRACK_PRICES trp
                           WHERE     trp.pgr_price_group_no = pgr.price_group_no
                                 AND trp.pgr_dep_dept_no = pgr.dep_dept_no
                                 AND trp.track_no = bpd.track_no
                                 AND trp.track_colour = J2_Constants_Pkg.default_track_colour)
                  AND cla.dep_dept_no = pgr.dep_dept_no
                  AND cla.class_no = bpd.class_no;

        mon_catch_boa ('L9', dbt_po_no, 'Inserted: ' || SQL%ROWCOUNT || ' records');
     EXCEPTION
        WHEN OTHERS
        THEN
           log_exception (dbt_po_no, dbt_ins_cla_prices);
           RAISE;
     END;
  ELSE
     mon_catch_boa ('L10', dbt_po_no, 'tracking level: ' || dbt_tracking_level);

     SELECT COUNT (*)
       INTO mon_grp
       FROM J2_PRICE_GROUPS
      WHERE dep_dept_no = dbt_dept_no;

     mon_catch_boa ('L11', dbt_po_no, mon_grp || ' groups found');

     SELECT COUNT (*)
       INTO mon_boa_det
       FROM B2_TMP_PURCHASE_ORDER_DET
      WHERE bph_po_no = dbt_po_no;

     mon_catch_boa ('L12', dbt_po_no, mon_boa_det || ' detail records found');

     BEGIN
        IF J2_Gst_Pkg.date_less_than_gst_cutover (SYSDATE)
        THEN
           n_rc := J2_Gst_Pkg.get_gst_default_rate (J2_Gst_Pkg.GST_RATE_NONE, n_rate_none);
           n_pre_gst := n_true;
        ELSE
           n_pre_gst := n_false;
        END IF;

        INSERT INTO J2_TRACK_PRICES (pgr_price_group_no,
                                     pgr_dep_dept_no,
                                     track_no,
                                     track_colour,
                                     cost_price,
                                     retail_price,
                                     selling_price,
                                     buying_price,
                                     ticket_retail_price,
                                     ticket_selling_price,
                                     cost_gst_rate,
                                     retail_gst_rate)
           SELECT DISTINCT
                  pgr.price_group_no,
                  pgr.dep_dept_no,
                  bpd.track_no,
                  bpd.track_colour,
                  bpd.cost_price,
                  DECODE (n_pre_gst,
                          n_true, bpd.retail_price,
                          J2_Gst_Pkg.calculate_bus_retail_price (bpd.retail_price, bpd.retail_gst_rate)),
                  DECODE (n_pre_gst,
                          n_true, bpd.retail_price,
                          J2_Gst_Pkg.calculate_bus_retail_price (bpd.retail_price, bpd.retail_gst_rate)),
                  bpd.cost_price,
                  bpd.retail_price,
                  bpd.retail_price,
                  DECODE (n_pre_gst, n_true, n_rate_none, bpd.cost_gst_rate),
                  DECODE (n_pre_gst, n_true, n_rate_none, bpd.retail_gst_rate)
             FROM B2_TMP_PURCHASE_ORDER_DET bpd, J2_PRICE_GROUPS pgr
            WHERE     bpd.bph_po_no = dbt_po_no
                  AND pgr.dep_dept_no = dbt_dept_no
                  AND bpd.line_no IN (SELECT MIN (bpd2.line_no)
                                        FROM B2_TMP_PURCHASE_ORDER_DET bpd2
                                       WHERE bpd2.bph_po_no = bpd.bph_po_no
                                      GROUP BY bpd2.track_no, bpd2.track_colour)
                  AND NOT EXISTS
                         (SELECT 1
                            FROM J2_TRACK_PRICES trp
                           WHERE     trp.pgr_price_group_no = pgr.price_group_no
                                 AND trp.pgr_dep_dept_no = pgr.dep_dept_no
                                 AND trp.track_no = bpd.track_no
                                 AND trp.track_colour = bpd.track_colour);

        mon_catch_boa ('L13', dbt_po_no, 'Inserted: ' || SQL%ROWCOUNT || ' records');
     EXCEPTION
        WHEN OTHERS
        THEN
           log_exception (dbt_po_no, dbt_ins_trk_prices);
           RAISE;
     END;
  END IF;
END create_track_prices;

在VBA中调用上述过程,当用户运行VBA代码时,步骤13显示已插入0行。有趣的是,如果我让用户再次运行相同的过程,步骤13显示插入72行作为我的预期。以下是我保存在日志表中的日志

6/03/2015 15:52:15 L2 - PO:F579693 - 插入曲目价格

6/03/2015 15:52:15 L10 - 采购订单:F579693 - 跟踪级别:ST

6/03/2015 15:52:15 L11 - PO:F579693 - 找到4组

6/03/2015 15:52:15 L12 - PO:F579693 - 找到82个明细记录

6/03/2015 15:52:15 L13 - PO:F579693 - 已插入:0记录

6/03/2015 15:52:15 L4 - 采购订单:F579693 - 已完成插入轨道价格

10/03/2015 09:51:04 L2 - PO:F579693 - 插入曲目价格

10/03/2015 09:51:04 L10 - 采购订单:F579693 - 跟踪级别:ST

10/03/2015 09:51:04 L11 - PO:F579693 - 找到4组

10/03/2015 09:51:04 L12 - PO:F579693 - 找到82个明细记录

10/03/2015 09:51:04 L13 - PO:F579693 - 已插入:72条记录

10/03/2015 09:51:04 L4 - 采购订单:F579693 - 已完成插入轨道价格