title: Oracle MERGE - if not matched then update if condition passes
body: <p>I need to merge some values into a table, updating a field when a row with the specified key already exists, or inserting a new row if it doesn't exist.</p>
  
  <p>This is my table:</p>
  
  <pre><code>profiles(name, surname, active);
  </code></pre>
  
  <p>where:</p>
  
  <pre><code>name    VARCHAR2(30)
  surname VARCHAR2(30)
  active  NUMBER(1)
  
  name and surname -&gt; composite primary key
  </code></pre>
  
  <p>I'm using this query:</p>
  
  <pre class="lang-sql prettyprint-override"><code>MERGE INTO profiles USING (
      SELECT
          'Mark' myName,
          'Zibi' mySurname,
          '1'    myActive
     FROM DUAL
  ) ON (
     name = myName
     AND surname = mySurname
  )
  WHEN MATCHED THEN
      UPDATE SET
          active = myActive
  WHEN NOT MATCHED THEN
      INSERT (
          name,
          surname,
          active
      ) VALUES (
          myName,
          mySurname,
          myActive
      );
  </code></pre>
  
  <p>It works, but it updates a record even if <code>active</code> is already set to <code>1</code>.</p>
  
  <p>What I would like to do is something like this:</p>
  
  <pre><code>WHEN MATCHED THEN
      IF(active != myActive)
          UPDATE SET
              active = myActive
      ELSE
          RAISE CUSTOM EXCEPTION
  WHEN NOT MATCHED THEN
      INSERT [...]
  </code></pre>
  
  <p>Is that possible? AFAIK I cannot put an <code>if</code> like this into a <code>MERGE</code> statement, so how could it be done?</p>