Update mit self-join
-
24-09-2019 - |
Frage
Ich möchte die Tabelle aktualisiert, um anzuzeigen, dass einige Zeilen sind die Eltern von andere, so dass ich fügte hinzu, ein "parentid" - Spalte, um die Tabelle.Die folgende Abfrage findet alle Eltern:
SELECT ca1.id, ca2.id
FROM contactassociations ca1
JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
where ca1.entitytable = 'EMPLOYER' AND
ca2.entitytable = 'CLIENT';
aber wenn ich versuche, mich anzupassen, die syntax zu tun, das update, es funktioniert nicht:
UPDATE contactassociations ca1
SET ca1.parentid = ca2.id
JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT';
Ich bekomme:
Error starting at line 6 in command:
UPDATE contactassociations ca1
SET ca1.parentid = ca2.id
JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT'
Error at Command Line:7 Column:28
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Beachten Sie, dass Zeile 7 Spalte 28 ist das Ende der "SET" - Zeile.
Lösung
Oracle nicht JOIN
Klausel in UPDATE
Anweisungen unterstützen.
Verwenden Sie diese:
MERGE
INTO contactassociations ca1
USING contactassociations ca2
ON (
ca1.contactid = ca2.contactid
AND ca1.entitytable = 'EMPLOYER'
AND ca2.entitytable = 'CLIENT'
)
WHEN MATCHED THEN
UPDATE
SET parentid = ca2.id
Andere Tipps
Ich finde den folgenden Stil, einfacher zu Lesen, aber Sie brauchen, um einen alias zu verwenden, nachdem der UPDATE-Schlüssel Wort, nicht der name der Tabelle:
UPDATE ca1
SET ca1.parentid = ca2.id
FROM contactassociations ca1
LEFT JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT'
-- Method #1
update emp set MANAGERNAME= mgr.EMPNAME
FROM SelfJoinTable emp , SelfJoinTable mgr where emp.MANAGERID = mgr.EMPID
-- Method #2
update emp
set MANAGERNAME= mgr.EMPNAME
FROM SelfJoinTable emp
LEFT OUTER JOIN SelfJoinTable mgr
ON emp.MANAGERID = mgr.EMPID
-- Method #3
update emp
set MANAGERNAME= mgr.EMPNAME
FROM SelfJoinTable emp
JOIN SelfJoinTable mgr
ON emp.MANAGERID = mgr.EMPID
-- Method #4
update emp
set MANAGERNAME= mgr.EMPNAME
FROM SelfJoinTable emp
inner JOIN SelfJoinTable mgr
ON emp.MANAGERID = mgr.EMPID
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow