sql - insert if not exists

后端 未结 4 1711
别那么骄傲
别那么骄傲 2021-01-13 08:44

I am having trouble with a sql query. I need to insert a row if the same row does not exist already. This is what I have so far:

DECLARE
BEGIN
   FOR FOLDE         


        
4条回答
  •  离开以前
    2021-01-13 08:59

    Do it all in SQL rather than context switching into PL/SQL:

    INSERT INTO DATA1.FOLDERS
    (folder_id,
     user_id)
    SELECT f1.folder_id,
           f1.user_id
      FROM DATA1.FOLDERS f1
     WHERE NOT EXISTS (SELECT 1
                         FROM DATA1.FOLDERS f2
                        WHERE f1.folder_id = f2.folder_id
                          AND f1.user_id = f2.user_id);
    

提交回复
热议问题