-
SQL Update
moin, Ich stehe voll auf dem schlauch ...
Datei1, in die der Update laufen soll:
Key: K1, K2, K3, JJ, MM, TT, S1 Update in Feld RG
Datei2 aus der ich Daten brauche für die Verknüpfung
Key: K1, K2, K3, JJ, MM, TT, S1 benötigtes Feld für die verknüpfung: S2
Datei3, aus der die update werte kommen
Key: K1, K2, K3, JJ, MM, TT, S2 RG1 und RG2
Die where Bedingung kann nur auf Datei 2 laufen.
Code:
Update datei1 a
set RG = 333000000 + (select RG1*100+RG2
from datei3 c
Where a.k1=c.k1 and a.k2=c.k2 and a.k3=c.k3 and
a.jj=c.jj and a.mm=c.mm and a.tt=c.tt and
Hier muß jetzt mit den Keys von Datei1 aus Datei2 der s2 Wert geholt werden und mit c.s1 verknüpft
Irgendwas passt nicht!
Danke
der ILEMax
-
Wenn aus mehreren Dateien upgedated wird, muss sichergestellt werden, dass es zu jedem Satz, der upgedated wird GENAU einen Satz in den abh. Tabellen gibt.
Zuerst setzt man sicherheitshalber einen SELECT DISTINCT und dann muss eine WHERE-Bedingung mit einer EXISTS-Anweisung hinzugefügt werden.
Die volle Syntax sieht also wie folgt aus:
Code:
update MyTable2 A
set (A.Feld1, A.Feld2) = (select Distinct B.Feld1, B.Feld2
from Mytable1 B
where A.Key1 = B.Key1
and A.Key2 = B.Key2)
where exists (select C.Key1, C.Key2
from MyTable1 C
where A.Key1 = C.Key1
and A.Key2 = C.Key2)
Wenn ein JOIN in dem SELECT-Statement erforderlich ist, muss der gleiche JOIN auch im SELECT von EXISTS hinzugefügt werden.
-
Danke, aber das war jetzt nicht so das Thema. Habe mich vllt schlecht ausgedrückt.
meine Lösung ist:
Code:
Update datei1 a
set RG = 333000000 + (select c.RG1*100+c.RG2
from datei3 c, datei2 b
Where a.k1=c.k1 and a.k2=c.k2 and a.k3=c.k3 and
a.jj=c.jj and a.mm=c.mm and a.tt=c.tt and
b.k1=c.k1 and b.k2=c.k2 and b.k3=c.k3 and
b.jj=c.jj and b.mm=c.mm and b.tt=c.tt and
c.S1=b.s2 and a.S1=b.S1
where (a.k1, a.k2, a.k3, a.jj, a.mm, a.tt, a.s1) in( select k1, k2, k3, jj, mm, tt, s1 from Datei2 where feld1 = wert1 and feld2 = wert2 and ...)
and a.RG = 0
-
Syntaktisch solltest du den Join nehmen und nicht mehr die Komma-Variante, dann wird es auch klarer.
from datei3 c inner join datei2 b on c.key = b.key .....
Und folge Birgittas Tipp, die Where-Klausel in Exists mit genau denselben Bedingungen zu scheiben:
where exists (
select *
from datei3 c inner join datei2 b on c.S1=b.s2
Where a.k1=c.k1 and a.k2=c.k2 and a.k3=c.k3 and
a.jj=c.jj and a.mm=c.mm and a.tt=c.tt and
b.k1=c.k1 and b.k2=c.k2 and b.k3=c.k3 and
b.jj=c.jj and b.mm=c.mm and b.tt=c.tt and
a.S1=b.S1
)
Im Exists reicht ein "*", da die Feldliste nicht benötigt wird.
Similar Threads
-
By KB in forum IBM i Hauptforum
Antworten: 7
Letzter Beitrag: 28-04-16, 14:42
-
By loeweadolf in forum NEWSboard Programmierung
Antworten: 2
Letzter Beitrag: 01-06-06, 09:43
-
By Joe in forum IBM i Hauptforum
Antworten: 2
Letzter Beitrag: 01-04-03, 19:50
-
By infomio in forum IBM i Hauptforum
Antworten: 3
Letzter Beitrag: 17-12-01, 06:30
-
By schreibr in forum IBM i Hauptforum
Antworten: 4
Letzter Beitrag: 12-09-01, 08:22
Berechtigungen
- Neue Themen erstellen: Nein
- Themen beantworten: Nein
- You may not post attachments
- You may not edit your posts
-
Foren-Regeln
|
Erweiterte Foren Suche
Google Foren Suche
Forum & Artikel Update eMail
AS/400 / IBM i
Server Expert Gruppen
Unternehmens IT
|
Kategorien online Artikel
- Big Data, Analytics, BI, MIS
- Cloud, Social Media, Devices
- DMS, Archivierung, Druck
- ERP + Add-ons, Business Software
- Hochverfügbarkeit
- Human Resources, Personal
- IBM Announcements
- IT-Karikaturen
- Leitartikel
- Load`n`go
- Messen, Veranstaltungen
- NEWSolutions Dossiers
- Programmierung
- Security
- Software Development + Change Mgmt.
- Solutions & Provider
- Speicher – Storage
- Strategische Berichte
- Systemmanagement
- Tools, Hot-Tips
Auf dem Laufenden bleiben
|
Bookmarks