简体   繁体   中英

with sql how can i insert data but if it's already exists update data

I want to add it - if exists update else insert!

so that insert is working but i want to only insert new value and if thing already here i want to update it. my code here only inserts data no metter what

BEGIN
DECLARE das int;
SELECT dasaxeleba.id INTO das FROM dasaxeleba WHERE dasaxeleba.barcode = NEW.barcode;
INSERT INTO sawyobi(das_id,raodenoba,tvitgirebuleba,gasayidi1,gasayidi2) 
VALUES(das,new.raodenoba,new.fasi,new.gasayidi1,new.gasayidi2);
END

if you are using oracle then you can use something like that:

       merge into sawyobi tgt
        using (select id
                 from dasaxeleba
                where barcode = NEW.barcode) src on (tgt.das_id = src.id)
 when matched then update set
              raodenoba = new.raodenoba,
              tvitgirebuleba = new.fasi,
              gasayidi1 = new.gasayidi1,
              gasayidi2 = new.gasayidi2
     when not matched then insert
              (das_id,
               raodenoba,
               tvitgirebuleba,
               gasayidi1,
               gasayidi2)
       values (src.id,
               new.raodenoba,
               new.fasi,
               ew.gasayidi1,
               new.gasayidi2)

not quite sure where your "new"-values come from... but i hope you get the idea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM