insert if not exists oracle

后端 未结 10 2074
天命终不由人
天命终不由人 2020-12-07 23:08

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert.

10条回答
  •  一整个雨季
    2020-12-07 23:28

    DECLARE
       tmp NUMBER(3,1);
    BEGIN
      SELECT COUNT(content_id) INTO tmp FROM contents WHERE (condition);
      if tmp != 0 then
        INSERT INTO contents VALUES (...);
      else
        INSERT INTO contents VALUES (...);
      end if;
    END;
    

    I used the code above. It is long, but, simple and worked for me. Similar, to Micheal's code.

提交回复
热议问题