Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy a subset of column data from one table to another
    text
    copied!<p>I have two tables with identical schema. Let's name them <strong>TestTable</strong> and <strong>TestTableTemp</strong>. I need to copy just two columns from TestTableTemp to TestTable without disrupting other data. The rows in TestTable are a subset of those in TestTableTemp. Let's say the columns that I need to copy are named <strong>Column1</strong> and <strong>Column2</strong> and that they have identical primary keys reference by column <strong>primaryKey</strong>.</p> <hr> <p>In mysql I believe this could be done as such or something similar:</p> <pre><code>UPDATE TestTable, TestTableTemp SET TestTable.Column1 = TestTableTemp.Column1, TestTable.Column2 = TestTableTemp.Column2 WHERE TestTable.primaryKey = TestTableTemp.primaryKey </code></pre> <p>Sqlite does not allow for multiple tables to be defined on the update statement as can been seen in their reference data here: <a href="http://www.sqlite.org/lang_update.html" rel="nofollow">http://www.sqlite.org/lang_update.html</a></p> <p>The best I could come up with is such:</p> <pre><code>UPDATE TestTable SET Column1 = (select TestTableTemp.Column1 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey), Column2 = (select TestTableTemp.Column2 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey) WHERE EXISTS(select * from TestTableTemp where TestTable.primaryKey = TestTableTemp.primaryKey" </code></pre> <hr> <p>This gives me a syntax error near "." I am guessing this is because I cannot reference TestTable in the scalar expressions.</p> <p>Can anyone point me in the right direction? Any help is much appreciated.</p> <p>EDIT:</p> <p>I cleaned up the second query a bit. It seems to just set the Column1 and Column2 to the first row from that column from TestTableTemp.</p>
 

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