[英]ADO.NET: Getting data from 1 table to another?
Does anyone know or have an example of getting data from 1 table to another? 有没有人知道或有一个从1个表到另一个表的数据的例子?
it is using an SQL SERVER EXPRESS, and 2 tables are practically the same, 1 was used as a temporary table by a fronend client.. so my backend client needs to move items from the temp tables to my other tables. 它使用的是SQL SERVER EXPRESS,并且2个表实际上是相同的,1被fronend客户端用作临时表..所以我的后端客户端需要将项目从临时表移动到我的其他表。
Do I need to use a forward only cursor and a for loop? 我是否需要使用仅向前游标和for循环?
I presume i open a connection, use a command object to query the database that would come back to a DataSet
? 我假设我打开一个连接,使用命令对象查询将返回
DataSet
的数据库?
I iterate over the for loop and call insert records using the COMMAND
object of ADO.NET
我遍历for循环并使用
ADO.NET
的COMMAND
对象调用插入记录
Is there not a better way of doing it ? 有没有更好的方法呢?
If the columns of the two tables are the same: 如果两个表的列相同:
INSERT INTO TABLE_1
SELECT * FROM TABLE_2
If not all the columns are the same, you can do it like this: 如果不是所有列都相同,您可以这样做:
INSERT INTO TABLE_1 (common_column1, common_column2, common_column3)
SELECT common_column1, common_column2, common_column3 FROM TABLE_2
After that you can update the columns that are not equal. 之后,您可以更新不相等的列。 Or you can insert a hardcoded value on the columns that aren't equal:
或者,您可以在不相等的列上插入硬编码值:
INSERT INTO TABLE_1 (common_column1, common_column2, common_column3, diff_column_1, diff_column_2)
SELECT common_column1, common_column2, common_column3, '1', '2' FROM TABLE_2
You can do this in SQL only, something like: 您只能在SQL中执行此操作,例如:
SELECT *
INTO theNewTable
FROM FirstTable dept
WHERE 1 = 1;
Put into a strored procedure or execute it directly as a command. 放入一个strored过程或直接作为命令执行它。
Note that: the INTO
will creates a new table with the same structure as the first one and copy all the rows from the first table into it. 请注意:
INTO
将创建一个与第一个表结构相同的新表,并将第一个表中的所有行复制到其中。 If you want to create only the table not to populate the rows and copy them into the new created table us a always-falsy expression. 如果您只想创建表而不填充行并将它们复制到新创建的表中,请使用始终为假的表达式。 for example "1 = 0".
例如“1 = 0”。
You can use DataTable.Copy
您可以使用
DataTable.Copy
http://msdn.microsoft.com/fr-fr/library/system.data.datatable.copy.aspx http://msdn.microsoft.com/fr-fr/library/system.data.datatable.copy.aspx
You can also use DataTable.ImportRow
您还可以使用
DataTable.ImportRow
Link : http://msdn.microsoft.com/fr-fr/library/system.data.datatable.importrow.aspx 链接: http : //msdn.microsoft.com/fr-fr/library/system.data.datatable.importrow.aspx
TSQL : You can also use : INSERT SELECT
query in stored procedure. TSQL:您还可以在存储过程中使用:
INSERT SELECT
查询。
You can insert data using this 您可以使用此插入数据
INSERT INTO TABLE (columnname1, columnname2, columnname3,.....)
SELECT columnname1, columnname2, columnname3 FROM TBL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.