title: Copy data from one table to another with constraints in Oracle
body: <p>I have two tables in Oracle, defined as the following:</p>
  
  <p><strong>Location Data</strong></p>
  
  <pre><code>loc_id
  capacity
  loc_type
  </code></pre>
  
  <p><strong>User Data</strong></p>
  
  <pre><code>loc_id
  custom001
  </code></pre>
  
  <p>What I'm wanting to do is copy what's in location.capacity into user.custom001 if the following constraints are met: </p>
  
  <p>custom001 &lt;> capacity</p>
  
  <p>loc_type IN ('A','B')</p>
  
  <p>capacity is not NULL or 0</p>
  
  <p>Based on some other queries I've found on Stack Exchange, I've developed this: </p>
  
  <pre><code>UPDATE user u 
  SET u.custom001 = 
    (SELECT l.capacity
    FROM location l
    WHERE u.loc_id = l.loc_id
    AND l.capacity &lt;&gt; u.custom001
    AND l.loc_type IN ('110','210')
    AND l.capacity IS NOT NULL 
    AND l.capacity &lt;&gt; 0)
  WHERE exists (select capacity from location l WHERE l.loc_id = u.loc_id)
  </code></pre>
  
  <p>But it's not respecting the constraints and is updating most every row in the user table, most of them with NULLs.</p>
  
  <p>Where do I need to go from here?  </p>