Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize this UPDATE statement to not use cursors
    primarykey
    data
    text
    <pre><code>FOR v2 AS c2 CURSOR FOR SELECT he.MyPrimary, he.SomeCode, he.SomeName, pe.MyPrimary FROM SomeTable he INNER JOIN AnotherTable pe ON (he.ColOne = pe.FooOne AND he.ColTwo = pe.ColTwo AND he.ColThree = pe.FooOne AND he.SomeCode = pe.SomeCode) WHERE he.relevancy = 1 AND he.ColThree = '2011-01-05' AND he.ColFive = 9 DO UPDATE AnotherTable SET match = he.MyPrimary, FooTwo = he.SomeCode, SomeName = he.SomeName WHERE MyPrimary = pe.MyPrimary; END FOR; </code></pre> <p>I have the above code and I'm trying to do this without using cursors but I'm not sure how to do an <code>UPDATE</code> statement with an <code>INNER JOIN</code>. Basically, what I'd like to do is join two tables <code>SomeTable</code> and <code>AnotherTable</code> then based on some column values from <code>SomeTable</code>, copy the value to a similar column in <code>AnotherTable</code>. I'm using DB2.</p> <p><strong>EDIT:</strong> I was just looking into this: <a href="https://stackoverflow.com/questions/4184209/inner-join-in-update-sql-for-db2">INNER JOIN in UPDATE sql for DB2</a></p> <p>Would it make sense to do something like this instead:</p> <pre><code>UPDATE SomeTable pe SET match = (SELECT he.MyPrimary FROM SomeTable he WHERE he.ColOne = pe.FooOne AND he.ColTwo = pe.ColTwo AND he.ColThree = pe.FooOne AND he.SomeCode = pe.SomeCode ), FooTwo = (SELECT he.SomeCode FROM SomeTable he WHERE he.ColOne = pe.FooOne AND he.ColTwo = pe.ColTwo AND he.ColThree = pe.FooOne AND he.SomeCode = pe.SomeCode ) WHERE he.relevancy = 1 AND he.ColThree = '2011-01-05' AND he.ColFive = 9 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload