2

我有两个表 MANUAL_TRANSACTIONS 和 MANUAL_LIST_TEMP。我想要实现的是使用来自 MANUAL_LIST_TEMP 的信息更新 MANUAL_TRANSACTIONS。这里只有存在于 MANUAL_LIST_TEMP 表中的记录应该更新为 MANUAL_TRANSACTIONS。

我已经做了类似下面的事情,但以下语句的问题是它更新了 MANUAL_TRANSACTIONS 表中的每条记录。

UPDATE MANUAL_TRANSACTIONS 
SET ( "Age", "Assigned_To", "Attachments", "Comments", "Completed_Date_Time"
, "Content_Type", "Created", "Created_By","Cycle_Time (Crt to Complete)"
, "Cycle_Time (First reply)", "DISTRIBUTION_CHANNEL")=
        (SELECT MANUAL_LIST_TEMP."Age", MANUAL_LIST_TEMP."Assigned_To", 
        MANUAL_LIST_TEMP."Attachments", MANUAL_LIST_TEMP."Comments", 
        MANUAL_LIST_TEMP."Completed_Date_Time", MANUAL_LIST_TEMP."Content_Type",
        MANUAL_LIST_TEMP."Created", MANUAL_LIST_TEMP."Created_By", 
        MANUAL_LIST_TEMP."Cycle_Time (Crt to Complete)", 
        MANUAL_LIST_TEMP."Cycle_Time (First reply)", 
        MANUAL_LIST_TEMP."Distribution_Channel" 
        FROM MANUAL_LIST_TEMP 
        WHERE MANUAL_LIST_TEMP.ID = MANUAL_TRANSACTIONS.ID)
4

5 回答 5

3

您还应该将 WHERE 子句添加到您的 UPDATE 语句中:

UPDATE MANUAL_TRANSACTIONS 
SET ( "Age", "Assigned_To", "Attachments", "Comments", "Completed_Date_Time"
, "Content_Type", "Created", "Created_By","Cycle_Time (Crt to Complete)"
, "Cycle_Time (First reply)", "DISTRIBUTION_CHANNEL")=
        (SELECT MANUAL_LIST_TEMP."Age", MANUAL_LIST_TEMP."Assigned_To", 
        MANUAL_LIST_TEMP."Attachments", MANUAL_LIST_TEMP."Comments", 
        MANUAL_LIST_TEMP."Completed_Date_Time", MANUAL_LIST_TEMP."Content_Type",
        MANUAL_LIST_TEMP."Created", MANUAL_LIST_TEMP."Created_By", 
        MANUAL_LIST_TEMP."Cycle_Time (Crt to Complete)", 
        MANUAL_LIST_TEMP."Cycle_Time (First reply)", 
        MANUAL_LIST_TEMP."Distribution_Channel" 
        FROM MANUAL_LIST_TEMP 
        WHERE MANUAL_LIST_TEMP.ID = MANUAL_TRANSACTIONS.ID)
WHERE MANUAL_TRANSACTIONS.ID IN (SELECT MANUAL_LIST_TEMP.ID FROM MANUAL_LIST_TEMP);

为获得最佳结果,请将您的 UPDATE 转换为 MERGE 语句:

MERGE INTO MANUAL_TRANSACTIONS tgt
USING (
        SELECT MANUAL_LIST_TEMP.ID,
        MANUAL_LIST_TEMP."Age", MANUAL_LIST_TEMP."Assigned_To", 
        MANUAL_LIST_TEMP."Attachments", MANUAL_LIST_TEMP."Comments", 
        MANUAL_LIST_TEMP."Completed_Date_Time", MANUAL_LIST_TEMP."Content_Type",
        MANUAL_LIST_TEMP."Created", MANUAL_LIST_TEMP."Created_By", 
        MANUAL_LIST_TEMP."Cycle_Time (Crt to Complete)", 
        MANUAL_LIST_TEMP."Cycle_Time (First reply)", 
        MANUAL_LIST_TEMP."Distribution_Channel" 
        FROM MANUAL_LIST_TEMP
) src
ON (tgt.ID = src.ID)
WHEN MATCHED THEN UPDATE
SET tgt."Age" = src."Age"
  , tgt."Assigned_To" = src."Assigned_To"
  [...]

MERGE 语句只会更新MANUAL_TRANSACTIONS目标MANUAL_LIST_TEMP表)中与(表)中具有匹配行的行。

于 2013-08-07T10:32:14.630 回答
1

您还需要一个 UPDATE 的 where 子句

于 2013-08-07T10:28:26.157 回答
0

试试这个

 UPDATE MANUAL_TRANSACTIONS 
    Set Age = MANUAL_LIST_TEMP.AGE, ....
    From MANUAL_TRANSACTIONS 
    inner join MANUAL_LIST_TEMP on MANUAL_LIST_TEMP.ID = MANUAL_TRANSACTIONS.ID
于 2013-08-07T10:27:17.243 回答
0
UPDATE MANUAL_TRANSACTIONS mt 
  left join MANUAL_LIST_TEMP mlt 
  on mt.ID = mlt.ID set mt.Age=mlt.Age .....
于 2013-08-07T10:32:46.940 回答
0

请试试这个:

UPDATE MANUAL_TRANSACTIONS mt INNER JOIN MANUAL_LIST_TEMP mlt 
on mt.ID = mlt.ID set mt.Age=mlt.Age,mt.Assigned_To=mlt.Assigned_To, .....;
于 2015-06-30T12:09:35.887 回答