从一台服务器批量复制到另一台服务器
我有一种情况,我需要将部分数据从一台服务器复制到另一台服务器。表架构完全相同。我需要从源中移动部分数据,这些数据在目标表中可能可用,也可能不可用。我想到的解决方案是,使用 bcp 将数据导出到文本(或 .dat)文件,然后将该文件带到目的地,因为两者无法同时访问(不同的网络),然后将数据导入到目的地。我需要满足一些条件:
- 我只需要从表中导出数据列表,而不是整个数据。我的客户将向我提供需要从源转移到目的地的 ID。我的主表中有大约 3000 条记录,子表中也有相同的记录。我期望的是,只移动 300 条记录。
- 如果记录存在于目标中,客户端将指示是否忽略或覆盖案例。 90%的情况下,我们需要忽略记录而不覆盖,而是将记录记录在日志文件中。
请帮助我采取最佳方法。我想过使用带查询选项的 BCP 来过滤数据,但是在导入时,如何绕过插入现有记录?如果需要的话,我该如何覆盖?
I've one situation where I need to copy part of the data from one server to another. The table schema are exactly same. I need to move partial data from the source, which may or may not be available in the destination table. The solution I'm thinking is, use bcp to export data to a text(or .dat) file and then take that file to the destination as both are not accessible at the same time (Different network), then import the data to the destination. There are some conditions I need to satisfy:
- I need to export only a list of data from the table, not whole. My client is going to give me IDs which needs to be moved from source to destination. I've around 3000 records in the master table, and same in the child tables too. What I expect is, only 300 records to be moved.
- If the record exists in the destination, the client is going to instruct as whether to ignore or overwrite case to case. 90% of the time, we need to ignore the records without overwriting, but log the records in a log file.
Please help me with the best approach. I thought of using BCP with query option to filter the data, but while importing, how do I bypass inserting the existing records? How do I overwrite, if that is needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,BCPing 到表中是一个全有或全无的交易,您无法选择要引入的行。
我要做的是。 。 。
数据库,这将存储 ID
您需要移动的行数。你
现在可以 BCP 输出您想要的行
需要。
新的“正在进行的工作”表,以及
BCP 那里的行。
这将决定是否
WIP 行进入目的地
表,或没有。
希望这有帮助。
更新
我所说的进行中工作 (WIP) 表并不是指 #temp 表,您不能 BCP 到临时表中(至少如果您可以的话我会感到非常惊讶)。
我的意思是,您将创建一个与目标表具有相同结构的表,bcp 到该表中,将 WIP 行编写到目标表中,然后删除 WIP 表。
你还没有说你正在使用什么RDBMS,假设是SQL Server,类似于下面的(未经尝试的代码)。 。 。
更新2
好的,所以你可以插入到临时表中,我刚刚尝试过(前几天我没有时间,抱歉)。
关于主/详细记录的问题(我们现在离开原来问题的主题,如果我是你,我会针对这个主题提出一个新问题,你会得到比我的更多的答案
)可以编写一个 SP 来逐步添加新行。
因此,您要循环临时表中的行(这些行具有来自源数据库的原始 id),将该行插入到目标表中,使用 SCOPE_IDENTITY 获取新插入行的 id。现在您有了旧 ID 和新 ID,您可以创建一个插入语句,该语句将为详细信息行插入语句,例如 . 。 。
希望这对您有帮助[如果它对您有帮助,您可以投票赞成这个答案,即使这不是您要选择的答案:)]
谢谢
体重
Unfortunately BCPing into a table is an all or nothing deal, you can't select rows to bring in.
What I'd do is . . .
database, this will store the ID's
of the rows you need to move. You
can now BCP out the rows that you
need.
a new Work In Progress table, and
BCP the rows in there.
that will decide whether or not a
WIP row goes into the destination
table, or not.
Hope this helps.
Update
By work in progress (WIP) tables I don't mean #temp tables, you can't BCP into a temp table (at least I'd be very sprprised if you could).
I mean a table you'd create with the same structure of the destination table, bcp into that, script the WIP rows to the destination table then drop the WIP table.
You haven't said what RDBMS you're using, assuming SQL Server, something like the following (untried code) . . .
Update 2
OK, so you can insert into temporary tables, I just tried it (I didn't have time the other day, sorry).
On the problem of the master/detail records (and we're now moving off the subject of the original question, if I were you I'd open a new question for this topic, you'll get more answers than just mine)
You can write an SP that will step through the new rows to add.
So, you're looping through the rows in your temp table (these rows have the original id on them from the source database), insert that row into the Destination table, use SCOPE_IDENTITY to get the id of the newly inserted row. Now you have the old Id and the new ID, you can create an insert statement that will insert statement for the detail rows like . . .
Hope this helps [if it has helped you are allowed to upvote this answer, even if it's not the answer you're going to select :)]
Thanks
BW