【问题标题】:How to update columns in a table joined with other tables?如何更新与其他表连接的表中的列?
【发布时间】:2013-04-17 13:40:53
【问题描述】:

我想参考其他表来更新表的两列。执行脚本时显示错误。

错误:错误从命令的第 1 行开始:

UPDATE wb_costing_work_items,
       sa_sales_documents,
       sa_sales_document_items
   SET cwi_price_per_hour = sdi_price,
       cwi_amount = sdi_price * cwi_hours
 WHERE cwi_lo_id = sad_lo_id
   AND sdi_sad_id = sad_id
   AND sdi_wit_id = cwi_wit_id
   AND cwi_id = 1650833

命令行错误:1 列:28 错误报告:SQL 错误:ORA-00971: 缺少 SET 关键字 00971. 00000 - “缺少 SET 关键字”

SQL 语句

UPDATE wb_costing_work_items cwi,
       sa_sales_documents sad, 
       sa_sales_document_items sdi
   SET cwi.cwi_price_per_hour = sdi.sdi_price,
       cwi.cwi_amount = sdi.sdi_price * cwi.cwi_hours
 WHERE cwi.cwi_lo_id = sad.sad_lo_id
   AND sdi.sdi_sad_id = sad.sad_id
   AND sdi.sdi_wit_id = cwi.cwi_wit_id
   AND cwi.cwi_id = 1650855

【问题讨论】:

  • 您要更新哪些表?
  • 我想更新表 wb_costing_work_items 的两列值应该来自其他表

标签: sql oracle sql-update


【解决方案1】:

这绝对可以。

            UPDATE (SELECT cwi_price_per_hour,
                           sdi_price,
                           cwi_amount,
                           sdi_price,
                           cwi_hours
                      FROM wb_costing_work_items,
                           sa_sales_documents,
                           sa_sales_document_items
                     WHERE     cwi_lo_id = sad_lo_id
                           AND sdi_sad_id = sad_id
                           AND sdi_wit_id = cwi_wit_id
                           AND cwi_id = 1650833)
               SET cwi_price_per_hour = sdi_price, cwi_amount = sdi_price * cwi_hours

请为使用的表加上前缀列,以便人们可以轻松阅读您的查询。

【讨论】:

  • 我尝试执行它显示错误的语句行:12 列:19 错误报告:SQL 错误:ORA-01779:无法修改映射到非键保留表 01779.00000 的列- “无法修改映射到非保留键表的列” *原因:尝试插入或更新映射到非保留键表的连接视图的列。 *Action:直接修改底层基表。
【解决方案2】:

可能是这样的。

请注意,我不得不对哪个列属于哪个表进行了一些疯狂的猜测,因为您没有包含有关如何定义表的任何信息。

所以很可能我没有做对,您需要将查询调整为您的实际表结构。

merge into wb_costing_work_items  
using 
(
    select cwi.pk_column,  -- don't know what the PK of the wb_costing_work_items is due to lack of information
           sdi.sdi_price,  -- don't know if this column really belong to this table
           sdi.sdi_price * cwi.cwi_hours as total-- again unsure about column names due to lack of information
    FROM wb_costing_work_items cwi
      JOIN sa_sales_documents sad ON sad.sad_lo_id = cwi.cwi_lo_id -- not sure about these, due to lack of information
      JOIN sa_sales_document_items sdi 
        ON sdi.sad_id = sad.sdi_sad_id     -- not sure about these, due to lack of information
       AND sdi.sdi_wit_id = cwi.cwi_wit_id -- not sure about these, due to lack of information

) t ON (t.pk_column = w.pk_column) -- not sure about this, due to lack of information
when matched then 
 update 
    set cwi_price_per_hour = t.sdi_price,
        cwi_amount = t.total;

【讨论】:

  • 这里的别名“w”代表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多