merge into 語句是insert 與update語句的結合,可以同時實作update和insert的功能
Oracle在9i引入了merge指令,
通過這個merge你能夠在一個SQL語句中對一個表同時執行inserts和updates操作. 當然是update還是insert是依據于你的指定的條件判斷的,Merge into可以實作用B表來更新A表資料,如果A表中沒有,則把B表的資料插入A表. MERGE指令從一個或多個資料源中選擇行來updating或inserting到一個或多個表
一、merge into語句的文法。
MERGE INTO schema. table alias
USING { schema. table | views | query} alias
ON {(condition) }
WHEN MATCHED THEN
UPDATE SET {clause}
WHEN NOT MATCHED THEN
INSERT VALUES {clause};
--解析
INTO 子句
用于指定你所update或者Insert目的表。
USING 子句
用于指定你要update或者Insert的記錄的來源,它可能是一個表,視圖,子查詢。
ON Clause
用于目的表和源表(視圖,子查詢)的關聯,如果比對(或存在),則更新,否則插入。
merge_update_clause
用于寫update語句
merge_insert_clause
用于寫insert語句
簡單的說就是,判斷表中有沒有符合on()條件中的資料,有了就更新資料,沒有就插入資料。
有一個表T,有兩個字段a、b,我們想在表T中做Insert/Update,如果條件滿足,則更新T中b的值,否則在T中插入一條記錄。在Microsoft的SQL文法中,很簡單的一句判斷就可以了,SQL Server中的文法如下:
if exists(select 1 from T where T.a='1001' )
update T set T.b=2 Where T.a='1001'
else
insert into T(a,b) values('1001',2);
但是很明顯這個文法對于SQL隻能更改一條語句,并且Oracle不能使用.是以就有了Merge into(Oracle 9i引入的功能)文法
merge into 目标表 a
using 源表 b
on(a.條件字段1=b.條件字段1 and a.條件字段2=b.條件字段2 ……)
when matched then update set a.字段=b.字段 --目标表别稱a和源表别稱b都不要省略
when not matched then insert (a.字段1,a.字段2……)values(b.字段1,b.字段2……) --目标表别稱a可省略,源表别稱b不可省略
"在一個同時存在Insert和Update文法的Merge語句中,總共Insert/Update的記錄數,就是Using語句中"源表"的記錄數"。
源表b可能是一張表結構不同于a的表,有可能是一張建構相同表結構的臨時表,也有可能是我們自己組起來的資料.
對于前兩種很好了解。現在主要說一下元件資料。
對于Oracle有dual這個系統表很好建構,如下
MERGE INTO T T1
USING
(
SELECT '1001' AS a, 2 AS b FROM dual
) T2
ON ( T1.a=T2.a)
WHEN MATCHED THEN
UPDATE SET T1.b= T2.b
WHEN NOT MATCHED THEN
INSERT (a,b) VALUES(T2.a,T2.b);
在sql server中,不能使用dual這個系統變量來建立表,是以隻能換下思路用union all組裝資料或使用臨時表,另外using中可以使用的還有table表,view視圖,sub_query子查詢
USING (
SELECT '1001'C1,2 C2
union all
SELECT '1002'C1,3 C2
union all...
) T2
工作中的一個執行個體
public Message Saves(List<GoodsQuestionManageModel> models)
{
Message msg;
StringBuilder sbSourceSql = new StringBuilder();
if (models.Count > 0)//循環組織sql語句
{
int i = 1;
foreach (GoodsQuestionManageModel model in models)
{
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",
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);
if (models.Count > i++)
{
sbSourceSql.AppendLine(" union all");
}
}
string strSql = string.Format(@"merge into tb_e_GoodsQuestionManage t
using
(
{0}
)tSource
on (t.GoodsQuestionManageId=tSource.GoodsQuestionManageId)
when not matched then
insert values
(
tSource.GoodsId, tSource.OrderNumber, tSource.Sku, tSource.GoodsQuantity, tSource.QuestionQuantity, tSource.GoodsQuestionTypeId, tSource.Remarks, tSource.CreateUserId, tSource.CreateTime, tSource.LastUpdateUserId, tSource.LastUpdateTime, tSource.IsDelete
)
when matched then
update set
t.GoodsId=tSource.GoodsId,t.OrderNumber=tSource.OrderNumber,t.Sku=tSource.Sku,t.GoodsQuantity=tSource.GoodsQuantity,t.QuestionQuantity=tSource.QuestionQuantity,
t.GoodsQuestionTypeId=tSource.GoodsQuestionTypeId,t.Remarks=tSource.Remarks,t.LastUpdateUserId=tSource.LastUpdateUserId,t.LastUpdateTime=tSource.LastUpdateTime,t.IsDelete=tSource.IsDelete;", sbSourceSql.ToString());
int effectNum = SqlHelper.ExecuteNonQuery(strSql);
if (effectNum > 0)
{
msg = new Message(true, "儲存成功!");
}
else
{
msg = new Message(false, "儲存失敗!");
}
}
else
{
msg = new Message(false, "沒有資料,無需儲存!");
}
return msg;
}
Merge 的其他功能
Merge語句還有一個強大的功能是通過OUTPUT子句,可以将剛剛做過變動的資料進行輸出。我們在上面的Merge語句後加入OUTPUT子句
此時Merge操作完成後,将所變動的語句進行輸出
當然了,上面的Merge關鍵字後面使用了多個WHEN…THEN語句,而這個語句是可選的.也可以僅僅新增或是僅僅删除
我們還可以使用TOP關鍵字限制目标表被操作的行,如圖8所示。在圖2的語句基礎上加上了TOP關鍵字,我們看到隻有兩行被更新。
但僅僅是MATCHED這種限制條件往往不能滿足實際需求,我們可以在圖7那個語句的基礎上加上AND附加上額外的限制條件
merge into 效率很高,強烈建議使用,尤其是在一次性送出事務中,可以先建一個臨時表,更新完後,清空資料,這樣update鎖表的幾率很小了。
https://blog.csdn.net/spw55381155/article/details/79891305
https://www.cnblogs.com/yanglang/p/14248271.html