您可以分阶段处理这个...
1)备份数据库
2)创建临时SQL表来保存你的更新记录
create table Player_Miles_Updates (
PlayerId int not null,
Miles int null)
3)装载从文本文件中的记录到您的临时表
bulk insert Player_Miles_Updates
from 'c:\temp\myTextRecords.csv'
with
(
FIELDTERMINATOR =' ,',
ROWTERMINATOR = '\n'
)
4)开始交易
begin transaction
5)将新的数据
insert into Player_Miles
select PlayerId, Miles
from Player_Miles_Updates
where PlayerId not in (select PlayerId from Player_Miles)
6)更新现有数据
update Player_Miles
set Player_Miles.Miles = pmu.Miles
from Player_Miles pm join Player_Miles_Updates pmu on pm.Player_Id = pmu.Player_Id
7)选择几行,让你想发生知道是什么,发生了
select *
from Player_Miles
where Player_Id in (1,45,86,14,83) -- use id's that you have seen in the csv file
8a)如果一切顺利
commit transaction
8B),如果一切不顺利
rollback transaction
9)删除临时表
drop table Player_Miles_Updates
这是一个非常简单的工作来备份你的数据库,并将其还原到不同的实例。然后你可以练习你喜欢的所有东西,直到你确定你的过程,然后再试着使用你的脚本。 – paul 2014-10-08 15:25:59