Merge into用法總結


簡單的說就是,判斷表中有沒有符合on()條件中的數據,有了就更新數據,沒有就插入數據。  

有一個表T,有兩個字段a、b,我們想在表T中做Insert/Update,如果條件滿足,則更新T中b的值,否則在T中插入一條記錄。在Microsoft的SQL語法中,很簡單的一句判斷就可以了,SQL Server中的語法如下:  

 

  1. if exists( select 1 from T where T.a='1001' )
  2.  
  3. update T set T.b=2 Where T.a='1001'
  4.  
  5. else
  6.  
  7. insert into T(a,b) values('1001',2);

但是很明顯這個語法對於SQL只能更改一條語句,並且Oracle不能使用.所以就有了Merge intoOracle 9i引入的功能)語法 

  1. merge into 目標表 a
  2.  
  3. using 源表 b
  4.  
  5. on(a.條件字段1=b.條件字段1 and a.條件字段2=b.條件字段2 ……)
  6.  
  7. when matched then update set a.更新字段=b.字段
  8.  
  9. when not matched then insert into a(字段1,字段2……)values(值1,值2……)

 

  "在一個同時存在Insert和Update語法的Merge語句中,總共Insert/Update的記錄數,就是Using語句中"源表"的記錄數"

 源表b可能是一張表結構不同於a的表,有可能是一張構建相同表結構的臨時表,也有可能是我們自己組起來的數據.

對於前兩種很好理解。現在主要說一下組件數據。

對於Oracle有dual這個系統表很好構建,如下

 

  1. MERGE INTO T T1
  2.  
  3. USING (SELECT '1001' AS a, 2 AS b FROM dual) T2
  4.  
  5. ON ( T1.a=T2.a)
  6.  
  7. WHEN MATCHED THEN
  8.  
  9. UPDATE SET T1.b= T2.b
  10.  
  11. WHEN NOT MATCHED THEN
  12.  
  13. INSERT (a,b) VALUES(T2.a,T2.b);

 在sql server中,不能使用dual這個系統變量來創建表,所以只能換下思路用union all組裝數據或使用臨時表,另外using中可以使用的還有table表,view視圖,sub_query,子查詢

  1. USING (
  2. SELECT '1001'C1,2 C2
  3. union all
  4. SELECT '1002'C1,3 C2
  5. union all...
  6. ) T2

工作中的一個實例

  1. public Message Saves(List<GoodsQuestionManageModel> models)
  2. {
  3. Message msg;
  4. StringBuilder sbSourceSql = new StringBuilder();
  5. if (models.Count > 0)//循環組織sql語句
  6. {
  7. int i = 1;
  8. foreach (GoodsQuestionManageModel model in models)
  9. {
  10. sbSourceSql.AppendFormat( "select {0} GoodsQuestionManageId,{1} GoodsId,'{2}' OrderNumber,'{3}' Sku,{4} GoodsQuantity,{5} QuestionQuantity,{6} GoodsQuestionTypeId,'{7}' Remarks,{8} CreateUserId,'{9}' CreateTime,{10} LastUpdateUserId,'{11}' LastUpdateTime,{12} IsDelete \r\n",
  11. model.GoodsQuestionManageId, model.GoodsId, model.OrderNumber, model.Sku, model.GoodsQuantity, model.QuestionQuantity, model.GoodsQuestionTypeId, model.Remarks, GlobalModel.Instance.UserId, DateTime.Now, GlobalModel.Instance.UserId, DateTime.Now, model.IsDelete);
  12. if (models.Count > i++)
  13. {
  14. sbSourceSql.AppendLine( " union all");
  15. }
  16. }
  17.  
  18. string strSql = string.Format(@"merge into tb_e_GoodsQuestionManage t
  19. using
  20. (
  21. {0}
  22. )tSource
  23. on (t.GoodsQuestionManageId=tSource.GoodsQuestionManageId)
  24. when not matched then
  25. insert values
  26. (
  27. tSource.GoodsId, tSource.OrderNumber, tSource.Sku, tSource.GoodsQuantity, tSource.QuestionQuantity, tSource.GoodsQuestionTypeId, tSource.Remarks, tSource.CreateUserId, tSource.CreateTime, tSource.LastUpdateUserId, tSource.LastUpdateTime, tSource.IsDelete
  28. )
  29. when matched then
  30. update set
  31. t.GoodsId=tSource.GoodsId,t.OrderNumber=tSource.OrderNumber,t.Sku=tSource.Sku,t.GoodsQuantity=tSource.GoodsQuantity,t.QuestionQuantity=tSource.QuestionQuantity,
  32. t.GoodsQuestionTypeId=tSource.GoodsQuestionTypeId,t.Remarks=tSource.Remarks,t.LastUpdateUserId=tSource.LastUpdateUserId,t.LastUpdateTime=tSource.LastUpdateTime,t.IsDelete=tSource.IsDelete;", sbSourceSql.ToString());
  33. int effectNum = SqlHelper.ExecuteNonQuery(strSql);
  34. if (effectNum > 0)
  35. {
  36. msg = new Message(true, "保存成功!");
  37. }
  38. else
  39. {
  40. msg = new Message(false, "保存失敗!");
  41. }
  42. }
  43. else
  44. {
  45. msg = new Message(false, "沒有數據,無需保存!");
  46. }
  47. return msg;
  48. }


Merge 的其他功能

 Merge語句還有一個強大的功能是通過OUTPUT子句,可以將剛剛做過變動的數據進行輸出。我們在上面的Merge語句后加入OUTPUT子句

   

   此時Merge操作完成后,將所變動的語句進行輸出

   

   

   當然了,上面的Merge關鍵字后面使用了多個WHEN…THEN語句,而這個語句是可選的.也可以僅僅新增或是僅僅刪除

   

 

   

   我們還可以使用TOP關鍵字限制目標表被操作的行,如圖8所示。在圖2的語句基礎上加上了TOP關鍵字,我們看到只有兩行被更新。

   

   

    但僅僅是MATCHED這種限制條件往往不能滿足實際需求,我們可以在圖7那個語句的基礎上加上AND附加上額外的限制條件

    

 

 

merge into  效率很高,強烈建議使用,尤其是在一次性提交事務中,可以先建一個臨時表,更新完后,清空數據,這樣update鎖表的幾率很小了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM