SQL Server仅基于多个列插入新行

时间:2016-05-10 19:04:54

标签: sql-server tsql

我在搜索过但却找不到任何符合我目的的东西。我只需要从一个表插入另一个表中的唯一行。我有:

表1

id  name    bookid bookname start_date   end_date   rel_date   rel_id
1   horror  1221   rockys    04/01/2016  04/30/2016 05/01/2016 4545
2   horror  1331   elm       04/01/2016  04/30/2016 05/01/2016 5656

表2

id  name    bookid bookname start_date   end_date   rel_date   rel_id
1   horror  1221   rockys    04/01/2016  04/30/2016 05/01/2016 4545
2   horror  1441   elm       04/01/2016  04/30/2016 05/01/2016 5656

我需要在table1中插入table = id = 2的行,并从table1中删除id = 2的行,因为即使其余列匹配,bookid也是不同的。 我试过以下:

insert into table1
select * from table2
where not exists (select * from table2 where table1.id = table2.id
and table1.name = table2.name and table1.bookid = table2.bookid and
table1.bookname = table2.bookname and table1.start_date =  table2.start_date
and table1.end_date = table2.end_date and table1.rel_date =            table2.rel_date
and table1.rel_id = table2.rel_id)

我可以在一个sql块中完成所有这些操作吗?

3 个答案:

答案 0 :(得分:0)

如果您只关注从bookid更新table2值,则可以使用以下查询更改bookid的值

UPDATE t1 SET t1.bookid = t2.bookid
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id

如果您认为id列在两个表中不唯一,则可能需要考虑在JOIN中添加其他匹配列。

答案 1 :(得分:0)

你需要实现的目标是:

UPDATE T1
SET T1.bookid = T2.bookid
FROM Table1 T1
JOIN Table2 T2
    ON T1.ID = T2.ID

然而,要完全按照问题回答这个问题:

DELETE T1
FROM Table1 T1
JOIN Table2 T2
    ON T1.ID = T2.ID
    AND T1.bookid <> T2.bookid

INSERT INTO Table1
SELECT id, name, bookid, bookname, start_date, end_date, rel_date, rel_id
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
    ON T1.ID = T2.ID
    AND T1.bookid = T2.bookid
WHERE T1.id IS NULL

请注意,如果您的ID字段不是唯一的,则您需要在ON子句中添加其他条件。

答案 2 :(得分:0)

理论上,以下合并声明应该实现您的目标。

    MERGE table1 [Target]
    USING table2 [Source]
    ON ([Target].[name] = [Source].[name] 
            AND 
        [Target].[bookname] = [Source].[bookname] 
            AND 
        [Target].[start_date] = [Source].[start_date] 
            AND 
        [Target].[end_date] = [Source].[end_date] 
            AND 
        [Target].[rel_date] = [Source].[rel_date] 
            AND 
        [Target].[rel_id] = [Source].[rel_id]  
      )
    WHEN MATCHED AND ([Target].[bookid] <> [Source].[bookid]) THEN
        UPDATE
            SET  [Target].[name]             = [Source].[name]      
                ,[Target].[bookid]           = [Source].[bookid]        
                ,[Target].[bookname]         = [Source].[bookname]  
                ,[Target].[start_date]       = [Source].[start_date]    
                ,[Target].[end_date]         = [Source].[end_date]  
                ,[Target].[rel_date]         = [Source].[rel_date]  
                ,[Target].[rel_id]           = [Source].[rel_id]        
    WHEN NOT MATCHED  THEN
    INSERT(         
             [name]         
            ,[bookid]       
            ,[bookname]     
            ,[start_date]   
            ,[end_date]     
            ,[rel_date]     
            ,[rel_id]       
         )
    VALUES
    (       
             [Source].[name]            
            ,[Source].[bookid]      
            ,[Source].[bookname]        
            ,[Source].[start_date]  
            ,[Source].[end_date]        
            ,[Source].[rel_date]        
            ,[Source].[rel_id]  
    );

请注意,此方法存在一些风险和限制。如果您的[id]列具有唯一性约束,则应将其设置为标识列,否则将遇到唯一性违规错误。此外,如果table1中的[id]列值与table2中的[id]列不同,则merge语句将保留table1中的原始[id]值。

基本上,此查询只是使用table2中的匹配记录更新table1中的现有记录,并将table2中的新记录插入table1(如果它们尚不存在)。