在sql中复制数据时添加列
我正在使用 SqlBulkCopy 将一些记录从一个表批量插入到另一个表中。该查询使用 SqlDataReader 来获取数据。表之间最重要的一个区别(除了在映射中处理的列顺序之外)是目标表具有一个需要添加当前日期的日期列。该日期不在源表中。我怎样才能将其添加到当前运行良好的进程中?
当前的工作代码如下所示:
SqlCommand cmd = new SqlCommand("SELECT * from dbo.source", cn);
SqlDataReader rdr = cmd.ExecuteReader();
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.ColumnMappings.Add(0, 0);
copy.ColumnMappings.Add(1, 2);
copy.ColumnMappings.Add(3, 3);
copy.ColumnMappings.Add(2, 4);
copy.ColumnMappings.Add(5, 5);
copy.ColumnMappings.Add(14, 6);
copy.DestinationTableName = "destination";
copy.WriteToServer(rdr);
}
数据库是 sql 2008 ENT。
I'm using SqlBulkCopy to bulk insert some records from one table into another table. The query is using a SqlDataReader to get the data. The one difference that matters (besides column order which is handled in the mappings) between the tables is that the destination table has a date column into which the current date needs to be added. This date is not in the source table. How can I add this into the current process which is working fine minus this?
Current working code looks like this:
SqlCommand cmd = new SqlCommand("SELECT * from dbo.source", cn);
SqlDataReader rdr = cmd.ExecuteReader();
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.ColumnMappings.Add(0, 0);
copy.ColumnMappings.Add(1, 2);
copy.ColumnMappings.Add(3, 3);
copy.ColumnMappings.Add(2, 4);
copy.ColumnMappings.Add(5, 5);
copy.ColumnMappings.Add(14, 6);
copy.DestinationTableName = "destination";
copy.WriteToServer(rdr);
}
The DB is sql 2008 ENT.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其添加为由您的 SELECT 返回:
然后为其添加另一个 ColumnMapping 。
You could just add it to be returned by your SELECT:
And then just add another ColumnMapping for it.