执行立即命令

时间:2016-06-01 20:10:52

标签: oracle

我收到了错误消息 ORA-00900:无效的SQL语句 ORA-06512:第125行 00900. 00000 - “无效的SQL语句” 当我执行这个SQL时:

    set serveroutput on;
DECLARE smartsoftVersionItem   varchar2(20);
        maxScriptLevel         number;
        sessionNo              number;
        SQLCommand             varchar2(8000);

BEGIN

   SELECT MAX(software_version)
     INTO smartsoftVersionItem
     FROM s1_grainsmart_dbupdate;

   SELECT MAX(script)
     INTO maxScriptLevel
     FROM s1_grainsmart_dbupdate;

   SELECT MAX(session_no)
     INTO sessionNo
     FROM ssc_dbupdate;

   INSERT INTO ssc_dbupdate
       VALUES (seq_ssc_dbupdate.NEXTVAL, sessionNo, sysdate, smartsoftVersionItem, maxScriptLevel, Null, '17.00.01', 'ssc_ivc_transaction_detail_v', 'V');

   SQLCommand := 
      'CREATE VIEW ssc_ivc_transaction_detail_v
       AS
       SELECT ivc_transaction_detail.ivc_transaction_nbr,
              ivc_transaction_detail.plc_id,
              ivc_transaction_detail.commodity_id,
              ivc_transaction_detail.opening_date,
              ivc_transaction_detail.group_nbr,
              ivc_transaction_detail.sort_order,
              ivc_transaction_detail.transaction_source,
              ivc_transaction_detail.transaction_time,
              ivc_transaction_detail.quantity_lb,
              ROUND(ivc_transaction_detail.quantity_lb * quantity_uom.conv_factor / 10000, quantity_uom.to_nbr_of_decimals)
                                               AS quantity,
              ivc_transaction_detail.dollar_value,
              ivc_transaction_detail.add_by,
              ivc_transaction_detail.orderlog_plc_id,
              ivc_transaction_detail.orderlog_nbr,
              ivc_transaction_detail.quantity_entry_nbr,
              ivc_transaction_detail.contract_nbr,
              ivc_transaction_detail.vendor_customer_id,
              ivc_transaction_detail.transaction_note,
              ivc_transaction_detail.ivc_lot_nbr,
              ivc_transaction_detail.vehicle_disposition_nbr,
              ivc_transaction_detail.receipt_nbr,
              ivc_transaction_detail.adj_type_nbr,
              ivc_transaction_detail.inv_adj_nbr,
              ivc_transaction_detail.adj_quantity,
              ivc_transaction_detail.production_nbr,
              ivc_transaction_detail.bin_transaction_nbr,
              ivc_transaction_detail.prd_to_prd_processing_nbr,
              ivc_transaction_detail.oe_plc_id,
              ivc_transaction_detail.oe_order_nbr,
              ivc_transaction_detail.oe_line_item_nbr,
              ivc_transaction_detail.po_plc_id,
              ivc_transaction_detail.po_order_nbr,
              ivc_transaction_detail.po_line_item_nbr,
              ivc_transaction_detail.mps_contract_nbr,
              ivc_transaction_detail.production_oh_dollar_value,
              ivc_transaction_detail.transfer_in_product_cost,
              ivc_transaction_detail.ae_plc_id,
              ivc_transaction_detail.ae_accrual_nbr,
              ivc_transaction_detail.transfer_plc_id,
              ivc_transaction_detail.transfer_order_nbr,
              ivc_transaction_detail.transfer_sort_order,
              ivc_transaction_detail.exchange_rate,
              ivc_transaction_detail.from_currency_code,
      ';

       IF maxScriptLevel >= 1700000117 THEN        -- WO-1702
              SQLCommand := SQLCommand ||
              'ivc_transaction_detail.theoretical_variance_value,
               ivc_transaction_detail.dollar_value_basis,
               ivc_transaction_detail.dollar_value_futures,
               ivc_transaction_detail.dollar_value_discount,
               ivc_transaction_detail.dollar_value_other,
               ivc_transaction_detail.adj_futures_by,
               ivc_transaction_detail.iv_option_month,
               ivc_transaction_detail.after_adj_iv_option_month,
               ivc_transaction_detail.iv_roll_option_month_nbr,
               ivc_transaction_detail.market_futures_subs_flag,
               ivc_transaction_detail.zero_futures_subs_flag,
               ivc_transaction_detail.market_basis_subs_flag,
               ivc_transaction_detail.zero_basis_subs_flag,
              ';
       END IF;

       IF maxScriptLevel > 1700030087 THEN        -- WO-1840
              SQLCommand := SQLCommand ||
              'ivc_transaction_detail.dollar_value_custom1,
               ivc_transaction_detail.dollar_value_custom2,
              ';
       END IF;

       IF maxScriptLevel > 1800000091 THEN        -- WO-2404
              SQLCommand := SQLCommand ||
              'ivc_transaction_detail.transfer_receiving_nbr,
              ';
       END IF;

       IF maxScriptLevel > 1800020121 THEN           -- WO-3066
              SQLCommand := SQLCommand ||
              'ROUND(ivc_transaction_detail.cost_summary_quantity_lb * quantity_uom.conv_factor / 10000, quantity_uom.to_nbr_of_decimals)
                                               AS cost_summary_quantity,
              ivc_transaction_detail.cost_summary_dollar_value
              ';
       END IF;

       SQLCommand := SQLCommand ||
          ' FROM ivc_transaction_detail
               LEFT OUTER JOIN
                    (SELECT ssc_plc_commodity_uom_v.plc_id,
                            ssc_plc_commodity_uom_v.commodity_id,
                            ssc_plc_commodity_uom_v.conv_factor,
                            ssc_plc_commodity_uom_v.to_nbr_of_decimals
                       FROM ssc_plc_commodity_uom_v
                    )                          quantity_uom
                 ON quantity_uom.plc_id = ivc_transaction_detail.plc_id
                AND quantity_uom.commodity_id = ivc_transaction_detail.commodity_id
          ';

   DBMS_OUTPUT.PUT_LINE ('SQL Command = ' || SQLCommand );
   EXECUTE IMMEDIATE 'SQLCommand';

END;

SQL意味着动态;即:如果我达到了预定的脚本级别,我想要包含其他列,否则不会。

然而,如果我从我的SQLCommand变量获取输出SQL并运行它会创建视图。 我错过了一些明显的东西 - 我现在还没有看到它。

任何帮助将不胜感激。 默里

1 个答案:

答案 0 :(得分:3)

更改

EXECUTE IMMEDIATE 'SQLCommand';

EXECUTE IMMEDIATE SQLCommand; -- no single quotes