新手oracle更新语句错误

时间:2014-10-02 02:27:13

标签: sql oracle

oracle查询:

UPDATE AIRMODEL_NETWORK_SUMMARY 
SET CASES_PRODUCED = (SELECT DISTINCT PRDCTN_RUN_ACTL_CASE_QTY
                       FROM AIRMODEL_NETWORK_SUMMARY, HISTORY_PRODUCTION, PERIOD_TO_PROCESS
                      WHERE AIRMODEL_NETWORK_SUMMARY.FSCL_WK_IN_YR_NUM = HISTORY_PRODUCTION.FSCL_WK_IN_YR_NUM 
                        AND AIRMODEL_NETWORK_SUMMARY.FSCL_YR_NUM = HISTORY_PRODUCTION.FSCL_YR_NUM 
                        AND AIRMODEL_NETWORK_SUMMARY.LOC_ID = HISTORY_PRODUCTION.LOC_ID 
                        AND HISTORY_PRODUCTION.FSCL_WK_IN_YR_NUM = PERIOD_TO_PROCESS.FSCL_WK_IN_YR_NUM 
                        AND HISTORY_PRODUCTION.FSCL_YR_NUM = PERIOD_TO_PROCESS.FSCL_YR_NUM);

返回以下错误:

  

ORA-01427:单行子查询返回多行

另一次纠正尝试:

 MERGE INTO AIRMODEL_NETWORK_SUMMARY AIRMODEL_NETWORK_SUMMARY1 
  USING (SELECT DISTINCT PRDCTN_RUN_ACTL_CASE_QTY, 
           AIRMODEL_NETWORK_SUMMARY2.rowid AS r 
      FROM AIRMODEL_NETWORK_SUMMARY AIRMODEL_NETWORK_SUMMARY2 
           INNER JOIN HISTORY_PRODUCTION 
              ON AIRMODEL_NETWORK_SUMMARY2.FSCL_WK_IN_YR_NUM = HISTORY_PRODUCTION.FSCL_WK_IN_YR_NUM 
                 AND AIRMODEL_NETWORK_SUMMARY2.FSCL_YR_NUM = HISTORY_PRODUCTION.FSCL_YR_NUM 
                 AND AIRMODEL_NETWORK_SUMMARY2.LOC_ID = HISTORY_PRODUCTION.LOC_ID 
           INNER JOIN PERIOD_TO_PROCESS 
              ON HISTORY_PRODUCTION.FSCL_WK_IN_YR_NUM = PERIOD_TO_PROCESS.FSCL_WK_IN_YR_NUM 
                 AND HISTORY_PRODUCTION.FSCL_YR_NUM = PERIOD_TO_PROCESS.FSCL_YR_NUM) 
   ON (AIRMODEL_NETWORK_SUMMARY1.rowid = r) 
 WHEN MATCHED THEN UPDATE 
  SET CASES_PRODUCED = PRDCTN_RUN_ACTL_CASE_QTY;

返回以下错误:

  

ORA-30926:无法在源表中获得一组稳定的行

我是一个菜鸟,需要帮助:(

谢谢。

1 个答案:

答案 0 :(得分:2)

您只能为CASES_PRODUCED分配单个标量值。所以你的子查询只需要生成一个标量值。

您需要找到子查询返回多行的原因。

SELECT DISTINCT ...将选择唯一的行,与ONE ROW不同。

您是否需要行中所有PRDCTN_RUN_ACTL_CASE_QTY的总和?

或平均值?

或MAX号码?

还是第一排?

如果是这样,请使用聚合函数:

总计

UPDATE AIRMODEL_NETWORK_SUMMARY 
    SET CASES_PRODUCED = (SELECT SUM(PRDCTN_RUN_ACTL_CASE_QTY) ...

最高

UPDATE AIRMODEL_NETWORK_SUMMARY 
    SET CASES_PRODUCED = (SELECT MAX(PRDCTN_RUN_ACTL_CASE_QTY) ...

平均

UPDATE AIRMODEL_NETWORK_SUMMARY 
    SET CASES_PRODUCED = (SELECT AVG(PRDCTN_RUN_ACTL_CASE_QTY) ...

第一行

UPDATE AIRMODEL_NETWORK_SUMMARY 
    SET CASES_PRODUCED = (SELECT PRDCTN_RUN_ACTL_CASE_QTY ... 
                          FROM ... 
                          WHERE ROWNUM = 1)

至关重要,特别是在学习和语法挣扎时,你清楚地理解

  1. 查询所需内容
  2. 您的查询返回的内容
  3. 尝试一系列替代方案,直到获得成功,这是创建错误数据的可靠方法。

    我的建议是首先将子查询SELECT复制到另一个窗口并运行它,查看并理解结果。应该清楚它是一个多行结果。使用子查询,直到它返回正确的单个结果,然后将其重新插入更大的更新。