![]() |
Datenbank: MySQL • Version: 5.x • Zugriff über: Nativ
Bedingtes Insert/Update geht das?
Hallo Zusammen!
Gibt es ein bedingtes Insert / Update? Beispiel: Gegeben sei eine Datenbank mit 2 Feldern (Nr:Integer, D : Double) Ich möchte einen Insert machen, wenn Nr in der Datenbank nicht enthalten ist und einen Update machen, wenn Abs(D_alt-D_neu) > 0.02 Geht das in einem Execute Befehl? Mavarik |
AW: Bedingtes Insert/Update geht das?
|
AW: Bedingtes Insert/Update geht das?
Zitat:
Abs(D_alt-D_neu) > 0.02 |
AW: Bedingtes Insert/Update geht das?
|
AW: Bedingtes Insert/Update geht das?
|
AW: Bedingtes Insert/Update geht das?
Zitat:
|
AW: Bedingtes Insert/Update geht das?
Schade, daß MySQL nicht sowas wie RETURNING kennt :cry:, dann ginge sowas
SQL-Code:
UPDATE, wenn passend und wenn nicht TRUE, dann INSERT.
INSERT INTO Tabelle (Nr, D)
SELECT (:Nr, :D) WHERE NOT ( UPDATE Tabelle SET D = :D WHERE Nr = :Nr AND abs(D - :D) <= 0.02 RETURNING true -- hier wird es schwer ) Alternativ muß man eben Beides machen.
SQL-Code:
(zuerst UPDATE und dann INSERT, damit das UPDATE das grade geINSERTe nicht nochmal sinnlos UPDATEd)
UPDATE Tabelle
SET D = :D WHERE Nr = :Nr AND abs(D - :D) <= 0.02; INSERT INTO Tabelle (Nr, D) SELECT (:Nr, :D) WHERE NOT exists(SELECT * FROM Tabelle WHERE Nr = :Nr AND abs(D - :D) <= 0.02); |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:44 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz