merge into Courses D
using (select id,iCourse from Courses where id='17491312' and iCourse= 45) s
on (d.id = s.id and d.Icourse = s.iCourse)
when matched then update set d.NAMECOURSE = 'jii'
when not matched then insert (id,Icourse,NAMECOURSE) values('17491312',45,'jii')
/可以看出,你的语句是没有问题的。不过,MERGE 是根据 using 子句中的记录数来决定执行次数的,
如果你的查询结果是空的,那么merge就不会执行。你可以先执行一下这个查询:
select id,iCourse from Courses where id='17491312' and iCourse= 45;看看究竟有没有结果。可以确定:问题就在这个查询上!!

解决方案 »

  1.   

    merge into Courses D
              using (select id,iCourse from Courses where id='17491312' and iCourse= 45) s
              on (d.id = s.id and d.Icourse = s.iCourse)
              when matched then update set  d.NAMECOURSE = 'jii'
              when not matched then insert (id,Icourse,NAMECOURSE)   
                     values('17491312',45,'jii')这条语句看起来没有语法错误,但是有逻辑上的错误,因为using子句 引用的表和目标表是同一个表,如果(select id,iCourse from Courses where id='17491312' and iCourse= 45)有记录,则必然能匹配,则执行matched update分支,没记录则匹配条件都不成立,所以不可能走到not matched 的分支,也不可能走到matched 分支,不知道你这条语句是用来做什么的,难道只是用来测试merge的用法?
      

  2.   

    TO  duanzilin(寻) 不是测试,是使用.
    目地就是想要对一个表进行操作,如果数据存在则修改,如果不存在则插入.
    看来我要事先SELECT一次才行囝