天天看点

oracle中merge into用法解析

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