title: sql - insert if not exists
body: <p>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:</p>
  
  <pre><code>DECLARE
  BEGIN
     FOR FOLDER_ROW IN (SELECT FOLDERID, USERID FROM DATA1.FOLDERS)
        LOOP                       
            IF NOT EXISTS (SELECT * FROM DATA1.FOLDER_USER WHERE FOLDER_ID = FOLDER_ROW.FOLDERID AND USER_ID = FOLDER_ROW.USERID) 
            INSERT INTO DATA1.FOLDER_USER (FOLDER_ID, USER_ID) VALUES (FOLDER_ROW.FOLDERID, FOLDER_ROW.USERID);
       END LOOP;
      COMMIT;
  END;
  </code></pre>
  
  <p>I would not be very familiar with sql particularly the not exists syntax so when I execute I get the following error:</p>
  
  <blockquote>
    <p>ORA-06550: line 37, column 11: PLS-00103: Encountered the symbol
    "INSERT" when expecting one of the following:</p>
    
    <p>then and or</p>
  </blockquote>
  
  <p>The symbol "then" was substituted for "INSERT" to continue.</p>
  
  <blockquote>
  <pre><code>ORA-06550: line 38, column 10:
  PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
  
     if
  ORA-06550: line 40, column 5:
  PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
  
     end not pragma final instantiable order overriding static
     member constructor map
  </code></pre>
  </blockquote>