2016-04-29 77 views
2

我將XML文件數據上傳到SQL Server數據庫。當我再次導入同一文件時,所有數據行都會被複制。將數據導入SQL服務器時跳過重複項

我試圖使用DISTINCT當重複的行被刪除,但是當我導入時,數據行仍然被重複。

如何在使用DISTINCT方法將數據導入SQL Server數據庫時跳過重複項?

我的表:

Create table HallSeat 
(
    HallGroupID int, 
    ShowSeatID int, 
    Color nvarchar(15), 
    Price int, 
    SeatRow int,  
    SeatNumber int, 
    IsReserved bit 
) 

SQL DISTINCT聲明:

SELECT DISTINCT * 
INTO tempdb.dbo.tmpTable 
FROM HallSeat 

DELETE FROM HallSeat 

INSERT INTO HallSeat 
    SELECT * 
    FROM tempdb.dbo.tmpTable 

DROP TABLE tempdb.dbo.tmpTable 
+0

提供您要導入的示例數據。 –

+0

上傳了我導入的XML文件http://s000.tinyupload.com/index.php?file_id=00087989931748177566 – Paulius

回答

2

您可以使用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; 

enter image description here

+0

非常感謝richard345!這是完美的答案。 – Paulius

相關問題