• Welcome, Guest. Please login.
 
September 17, 2019, 11:08:19 am

News:

Welcome to the SQLitening support forums!


Copy rows with a new index?

Started by Bern Ertl, January 18, 2013, 12:50:00 pm

Previous topic - Next topic

Bern Ertl

January 18, 2013, 12:50:00 pm Last Edit: January 18, 2013, 12:52:15 pm by Bern Ertl
I've got a table with records that reference another table:

CREATE TABLE MyTable ( RowID INTEGER PRIMARY KEY,
                        DateData NOT NULL,
                        Data2,
                        FKey INTEGER NOT NULL REFERENCES AnotherTable( RowID) ON DELETE CASCADE,
                        UNIQUE ( DateData, FKey));


Let's say the table has 3 rows in it with FKey = 4.  I want to copy/insert those 3 rows into the same table, but with a new FKey ( = 7 for example). 

I know I can use a select statement to iterate through the 3 rows explicitly in my code and perform 3 separate inserts to accomplish my goal, but I was wondering if there was any way to achieve the same result with a single SQL statement?


Bern Ertl

I think sometimes just posting the question helps me see the answer.INSERT INTO MyTable (DateData, Data2, FKey) SELECT DateCata, Date2, 7 AS FKey FROM MyTable WHERE FKey=3