简体   繁体   English

使用左连接更新 Oracle

[英]Oracle update with a left join

I want to simply update a single table set of rows with parameters passed in and not from another table but to identify the rows, I need to do a left join.我想简单地使用传入的参数更新单个表的行集,而不是来自另一个表,但要识别行,我需要进行左连接。 Here is the query to select:这是要选择的查询:

SELECT *
FROM SAMPLE_STATUS pss
  LEFT JOIN QC q ON q.SAMPLE_ID = pss.SAMPLE_ID
WHERE q.CONTRACT_CLN_ID = 28 AND q.LOT = 1

I have tried several examples here is the last one with just one column我已经尝试了几个例子,这是最后一个只有一列

UPDATE 
  (SELECT pss.APP_WIN_START_DT, TO_CHAR(sysdate, 'YYYYMMDD') AS NEW_AWSD
  FROM SAMPLE_STATUS pss
    LEFT JOIN QC q ON q.SAMPLE_ID = pss.SAMPLE_ID
  WHERE q.CONTRACT_CLN_ID = 28 AND q.LOT = 1) pq
  SET APP_WIN_START_DT = NEW_AWSD

Several records are pulled from the QC table that join to the Sample_Status table via the sample id.从 QC 表中提取的几条记录通过样本 ID 连接到 Sample_Status 表。 The error I get is :我得到的错误是:

SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 -  "cannot modify a column which maps to a non key-preserved table"
*Cause:    An attempt was made to insert or update columns of a join view which
           map to a non-key-preserved table.
*Action:   Modify the underlying base tables directly.

Thanks in advance.提前致谢。

I think that you actually want inner join logic here, but in any case you may write this update using exists:我认为您实际上需要内部连接逻辑,但无论如何您都可以使用exists 编写此更新:

UPDATE SAMPLE_STATUS pss
SET APP_WIN_START_DT = TO_CHAR(sysdate, 'YYYYMMDD')
WHERE EXISTS (SELECT 1 FROM QC q
              WHERE q.SAMPLE_ID = pss.SAMPLE_ID AND
                    q.CONTRACT_CLN_ID = 28 AND q.LOT = 1);

The reason I suspect you don't want a left join here is that you are updating a field of the table on the left side of the join.我怀疑您不想要左连接的原因是您正在更新连接左侧的表字段。 But since you have restrictions in the where clause on the right side table, it would just behave like an inner join anyway.但是由于您在右侧表的 where 子句中有限制,所以无论如何它都表现得像内连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM