立即执行命令
Posted
技术标签:
【中文标题】立即执行命令【英文标题】:EXECUTE IMMEDIATE COMMAND 【发布时间】:2016-06-01 20:10:52 【问题描述】:我收到一个错误 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 并运行它会创建视图。 我遗漏了一些明显的东西 - 我现在看不到它。
我们将不胜感激。 穆雷
【问题讨论】:
删除SQLCommand
: EXECUTE IMMEDIATE SQLCommand;
周围的引号。否则,您将尝试从字面上执行 SQL 字符串 SQLCommand
:)
预测您的下一个错误。如果maxScriptLevel <= 1800020121
您生成的脚本在from
子句之前包含一个流氓逗号,那么它将失败,
是的,你是正确的..修复它。
【参考方案1】:
改变
EXECUTE IMMEDIATE 'SQLCommand';
到
EXECUTE IMMEDIATE SQLCommand; -- no single quotes
【讨论】:
以上是关于立即执行命令的主要内容,如果未能解决你的问题,请参考以下文章
BigQuery 是不是支持“立即执行”命令来运行动态查询?