您可以使用T-SQL MERGE語句來做到這一點。它將匹配您的HallSeat表導入的行集。如果該行不存在,它將插入一個新行。如果該行確實存在並且存在差異,則可以更新它。
(你可能不希望做刪除操作,但我已經包括它的完整性。)
請參閱聯機叢書>合併(的Transact-SQL) - https://msdn.microsoft.com/en-GB/library/bb510625.aspx
爲了證明這一點,第一創建兩個表。
CREATE TABLE dbo.HallSeat
(
HallGroupID int NOT NULL,
ShowSeatID int NOT NULL,
Color nvarchar(15) NOT NULL,
Price int NOT NULL,
SeatRow int NOT NULL,
SeatNumber int NOT NULL,
IsReserved bit NOT NULL,
CONSTRAINT PK_HallSeat PRIMARY KEY CLUSTERED (HallGroupID, ShowSeatID)
);
CREATE TABLE dbo.ImportHallSeat
(
HallGroupID int NOT NULL,
ShowSeatID int NOT NULL,
Color nvarchar(15) NOT NULL,
Price int NOT NULL,
SeatRow int NOT NULL,
SeatNumber int NOT NULL,
IsReserved bit NOT NULL,
CONSTRAINT PK_ImportHallSeat PRIMARY KEY CLUSTERED (HallGroupID, ShowSeatID)
);
然後將XML數據文件導入ImportHallSeat表:
-- Read the XML data file to be imported
DECLARE @xml xml;
SELECT @xml = x.a
FROM OPENROWSET(BULK 'F:\Work\Data.xml', SINGLE_BLOB) AS x(a);
TRUNCATE TABLE dbo.ImportHallSeat;
INSERT INTO dbo.ImportHallSeat(HallGroupID, ShowSeatID, Color, Price, SeatRow, SeatNumber, IsReserved)
SELECT T.C.value('HallGroupID[1]', 'int') AS 'HallGroupID',
T.C.value('ShowSeatID[1]', 'int') AS 'ShowSeatID',
T.C.value('Color[1]', 'nvarchar(15)') AS 'Color',
T.C.value('Price[1]', 'money') AS 'Price',
T.C.value('SeatRow[1]', 'int') AS 'SeatRow',
T.C.value('SeatNumber[1]', 'int') AS 'SeatNumber',
T.C.value('IsReserved[1]', 'bit') AS 'IsReserved'
FROM @xml.nodes(N'/Filharmonija/Hall/HallGroup/HallSeat') as T(C);
然後,我們可以更新HallSeat表導入的數據:
MERGE
INTO dbo.HallSeat AS H
USING dbo.ImportHallSeat AS I
ON I.HallGroupID = H.HallGroupID AND I.ShowSeatID = H.ShowSeatID
WHEN MATCHED AND H.Color <> I.Color AND H.Price <> I.Price
THEN UPDATE SET H.Color = I.Color, H.Price = I.Price
WHEN NOT MATCHED BY TARGET
THEN INSERT (HallGroupID, ShowSeatID, Color, Price, SeatRow, SeatNumber, IsReserved)
VALUES (I.HallGroupID, I.ShowSeatID, I.Color, I.Price, I.SeatRow, I.SeatNumber, I.IsReserved)
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
顯示擁有數據被輸入到HallSeat表中:
SELECT *
FROM dbo.HallSeat;
提供您要導入的示例數據。 –
上傳了我導入的XML文件http://s000.tinyupload.com/index.php?file_id=00087989931748177566 – Paulius