This question already has answers here:
SQL update query using joins (13 answers)
Closed last month.
I have two tables with multiple records in and I want to update one table's rows with a value from another tables records matched on ORD_NAME
and OP_NAME
.
My SQL so far:
INSERT INTO OPS_NOTREQUIRED (ORD_NAME, OP_NAME, STATUS)
VALUES ('1', '10', 'DELETE')
INSERT INTO OPS_NOTREQUIRED (ORD_NAME, OP_NAME, STATUS)
VALUES ('2', '10', 'DELETE')
INSERT INTO OPS_NOTREQUIRED (ORD_NAME, OP_NAME, STATUS)
VALUES ('3', '10', '')
INSERT INTO IMPORT (ORD_NAME, ORD_QTY, ORD_PART, ORD_PARTREV, ORD_DUEDATE, OP_NAME, EAI_Status)
VALUES ('1', '400', '1234', '1', '2023-12-01 09:10:04.530', '10', 'NULL')
INSERT INTO IMPORT (ORD_NAME, ORD_QTY, ORD_PART, ORD_PARTREV, ORD_DUEDATE, OP_NAME, EAI_Status)
VALUES ('2', '400', '1234', '1', '2023-12-01 09:10:04.530', '10', 'NULL')
INSERT INTO IMPORT (ORD_NAME, ORD_QTY, ORD_PART, ORD_PARTREV, ORD_DUEDATE, OP_NAME, EAI_Status)
VALUES ('3', '400', '1234', '1', '2023-12-01 09:10:04.530', '10', 'NULL')
UPDATE [IMPORT]
SET EAI_Status = 'DELETE'
WHERE (SELECT EAI_Status FROM OPS_NOTREQUIRED) = 'DELETE'
I want the IMPORT
table to have the same values as the OPS_NOTREQUIRED
table for column STATUS
, but I can't seem to figure it out.
2条答案
按热度按时间ncgqoxb01#
I found the merge command.
sxissh062#
This can be done using a regular inner join in an update statement:
Note the use of aliases that allow the
UPDATE
clause to conveniently reference the target table already referenced in theFROM
clause.