如果行不存在,则仅插入SQL

时间:2017-02-13 20:42:26

标签: sql sql-server

我有以下数据表。

TABLE_1

ID, GROUP_ID, ARTIFACT_ID, VERSION  
101, com.abc, pqra, 1.0.0  
102, com.abc, pqrb, 2.0.0  
103, com.abc, pqrc, 3.0.0  
104, com.abc, pqrd, 4.0.0 

TABLE_2

ID, MODULE_ID, ISSUE_KEY  
11, 104, XYZ-12  
12, 104, XYZ-34

我使用以下命令在Table_2中插入值。

INSERT INTO Table_2 (MODULE_ID, ISSUE_KEY) SELECT ID, 'XYZ-56' FROM Table_1
WHERE Table_1.GROUP_ID = 'com.abc' AND Table_1.ARTIFACT_ID = 'pqrd' AND 
Table_1.VERSION = '4.0.0'  

正如您可以从上面的查询中推断出来的,我首先尝试从TABLE_1获取给定IDGROUP_IDARTIFACT_ID值的VERSION然后插入数据到TABLE_2中,检索到的IDISSUE_KEY值为XYZ-56。 执行上述命令后,Table_2将显示为

ID, MODULE_ID, ISSUE_KEY  
11, 104, XYZ-12  
12, 104, XYZ-34
13, 104, XYZ-56

上述INSERT查询的问题在于,它不会检查是否已存在具有特定值MODULE_IDISSUE_KEY的给定行。例如,如果我再次执行相同的插入查询,那么该表将看起来像

ID, MODULE_ID, ISSUE_KEY  
11, 104, XYZ-12  
12, 104, XYZ-34
13, 104, XYZ-56
14, 104, XYZ-56

我希望仅在给定的MODULE_IDISSUE_KEY中没有行的情况下将行插入表2中。
有人可以帮我修改上面的插入查询以按预期执行。

4 个答案:

答案 0 :(得分:1)

您可以使用WHERE NOT EXISTS

执行此操作
Insert  Into Table_2 (MODULE_ID, ISSUE_KEY)
Select  ID, 'XYZ-56'
From    Table_1 T1
Where   T1.GROUP_ID = 'com.abc'
And     T1.ARTIFACT_ID = 'pqrd'
And     T1.VERSION = '4.0.0'
And Not Exists
(
    Select  *
    From    Table_2 T2
    Where   T2.Module_Id = T1.Id
    And     T2.Issue_Key = 'XYZ-56'
);

以下是使用CTELEFT JOIN的另一种方法:

;With ToInsert As
(
    Select  ID As Module_Id, 'XYZ-56' As Issue_Key
    From    Table_1 T1
    Where   T1.GROUP_ID = 'com.abc'
    And     T1.ARTIFACT_ID = 'pqrd'
    And     T1.VERSION = '4.0.0'
)
Insert      Table_2
            (Module_Id, Issue_Key)
Select      I.Module_Id, I.Issue_Key
From        ToInsert    I
Left Join   Table_2     T2  On  T2.Module_Id = I.Module_Id
                            And T2.Issue_Key = I.Issue_Key
Where       T2.Id Is Null;

答案 1 :(得分:0)

您可以使用如下所示的if块来检查记录是否存在:

 let link = "http://www.flickr.com/services/feeds/photos_public.gne?tags=swimming&format=json&nojsoncallback=1"

        let urlString = link

        let url = URL(string: urlString)
        URLSession.shared.dataTask(with:url!) { (data, response, error) in
            if error != nil {
                print(error)
            } else {
                do {

                    let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
                    let currentConditions = parsedData["items"] as! [[String:Any]]

                    print(currentConditions)

                    let currentItem = parsedData["link"] as! String
                    print(currentItem)

                } catch let error as NSError {
                    print(error)
                }
            }

            }.resume()

答案 2 :(得分:0)

您可以使用except

INSERT INTO Table_2 (MODULE_ID, ISSUE_KEY) 
SELECT ID, 'XYZ-56' 
FROM Table_1
WHERE Table_1.GROUP_ID = 'com.abc' 
  AND Table_1.ARTIFACT_ID = 'pqrd' 
  AND Table_1.VERSION = '4.0.0'  

except 
select MODULE_ID, ISSUE_KEY
from Table_2

not exists()

INSERT INTO Table_2 (MODULE_ID, ISSUE_KEY) 
SELECT ID, 'XYZ-56' 
FROM Table_1
WHERE Table_1.GROUP_ID = 'com.abc' 
  AND Table_1.ARTIFACT_ID = 'pqrd' 
  AND Table_1.VERSION = '4.0.0'  
  and not exists (
    select 1
    from table_2 as i
    where i.Module_ID = table_1.ID
      and i.Issue_Key = 'XYZ-56' 
  )

或其他类似的方法。

答案 3 :(得分:0)

您正在寻找Merge声明。

引用文档:

  

根据与源表的连接结果,对目标表执行插入,更新或删除操作。例如,您可以根据另一个表中的差异在一个表中插入,更新或删除行来同步两个表。

注意:仔细阅读性能提示。