简体   繁体   English

将数据从一个表转移到另一表

[英]Transfer data from one table to another table

Can anybody help me to create a good way to transfer data from one table to another table? 有人可以帮我创建一种将数据从一个表转移到另一表的好方法吗?

For example: 例如:

table1 表格1

ID | Name

1  | Juan
2  | Two

table2 表2

(no content)

What I want is a loop that will transfer the data of table1 to table2 . 我想要的是一个循环,该循环会将table1的数据传输到table2 While not all data of table1 is transferred to table2 the loop continues. 虽然并非将table2 table1所有数据都传输到table2但循环仍在继续。

The standard SQL approach is: 标准的SQL方法是:

insert into table2(id, name)
    select id, name
    from table1;

You don't need a loop. 您不需要循环。

I suppose you mean to do this in VB. 我想您的意思是在VB中执行此操作。 Let conn, rs1 and rs2 already initialized, you can obtain your goal as shown: 让conn,rs1和rs2已经初始化,您可以如下所示获得目标:

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()

Try this in sql 在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. 这两个查询之间的区别是,第一个查询需要在执行之前创建表。第二个查询将自动创建表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM