将数据导入SQL Server

时间:2016-04-29 08:37:21

标签: sql-server duplicates distinct

我将XML文件数据上传到SQL Server数据库。当我第二次导入同一个文件时,所有数据行都会重复。

我删除了重复的行时尝试使用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

1 个答案:

答案 0 :(得分:2)

您可以使用T-SQL MERGE语句执行此操作。它将与您的HallSeat表导入的行集匹配。如果该行不存在,它将插入一个新行。如果该行确实存在且存在差异,则可以更新它。

(您可能不想执行删除操作,但我已将其包含在内以保证完整性。)

请参阅联机丛书> MERGE(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