Reputation: 175
Can anybody help me to create a good way to transfer data from one table to another table?
For example:
table1
ID | Name
1 | Juan
2 | Two
table2
(no content)
What I want is a loop that will transfer the data of table1
to table2
. While not all data of table1
is transferred to table2
the loop continues.
Upvotes: 0
Views: 3131
Reputation: 28413
Try this in sql
Insert Into table2(id, name)
Select id, name
From table1
Where <Conditions to insert>
OR
Select * into <target_Table>
From table1
Where <Conditions to insert>
The difference between both the query is ,in first one you need to create table before execute.In second one it will automatically create the table.
Upvotes: 0
Reputation: 445
I suppose you mean to do this in VB. Let conn, rs1 and rs2 already initialized, you can obtain your goal as shown:
rs1.Open "Table1", conn
rs2.Open "Table2", conn, 3, 3
Do Until rs1.EOF
rs2.AddNew()
rs2("id") = rs1("id").Value
rs2("name") = rs1("name").Value
rs2.Update
rs1.MoveNext()
Loop
rs2.Close()
rs1.Close()
Upvotes: 0
Reputation: 1270431
The standard SQL approach is:
insert into table2(id, name)
select id, name
from table1;
You don't need a loop.
Upvotes: 1