-1

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
  • 3
    Your question is unclear. Please provide sample data, desired results, and an appropriate database tag. – Gordon Linoff Mar 17 '20 at 10:26
  • Please improve the quality of your question. I personally, do not understand what your question is. – user14063792468 Mar 17 '20 at 10:32
  • Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a [tag](https://stackoverflow.com/help/tagging) for the database product you are using. [Why should I tag my DBMS](https://meta.stackoverflow.com/questions/388759/) –  Mar 17 '20 at 10:48
  • In Postgres you can use `insert .. on conflict ...` for that. See [here](https://stackoverflow.com/questions/tagged/upsert+sql) for possible other solutions –  Mar 17 '20 at 10:48

1 Answers1

0

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

MarEll
  • 296
  • 2
  • 9