Search code examples
sqlsqlitedata-migration

Is it possible to copy some rows from one table to the other along with rowids?


If I type:

INSERT INTO table_b 
SELECT rowid, somecolumn 
  FROM table_a 

...the rowid column would copy into new table as ordinary column and this would most likely produce an error since columns wouldn't match.

But is there a way to copy exactly the same rowids from old table to new when I'm populating it fresh ?

I know it is possible to do it that way:

INSERT INTO table_b 
  (rowid, othercolumn) 
  VALUES (334,  'sometext')

...but that needs to be write row by row instead of one line sql command.


Solution

  • The first SQL you write is correct and will copy all information matching the columns. You can also use a query like this:

    INSERT INTO table2( rowId, rowValue)
    
    SELECT rowId, rowValue FROM table1