您可以分階段處理這個...
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