SqlBulkCopy insert order

自作多情 提交于 2019-12-05 19:34:05

It looks like there is no guarantee of order while bulk insert. So I added a temporary id column to the destination table. Flow will be as follows:

Step1: var dt = select *, id as tempId from server1.dbo.TableA order by id;
Step2: SQL bulk copy into server2 bulkCopy.WriteToServer(dt);
Step3: var resultDt = select top 4 id, tempId from server2.dbo.TableA order by id desc. Since we know the number of records we inserted I am using "top 4".

Now id will be the new id generated by server2 and tempId will be the id from server1. Problem solved :)

Another solution is available in the below link:

https://social.msdn.microsoft.com/Forums/en-US/ee3d90b0-d212-4bf0-8f2a-5dcb716896eb/sqlbulkcopy-insert-order?forum=adodotnetdataproviders

  • Create staging table(say tmp_tableA) with same structure of tableA without identity.
  • Bulk copy the data into tmp_tableA.
  • Now insert into tableA from tmp_tableA.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!