今天用ibatis寫個插入操作,為了兼容修改想使用 merge into語句,以便重復插入時直接 update,具體語句如下:
<insert id="wlf"> MERGE INTO t_wlf_info t USING dual ON(t. id=#id# and t.channel=#channel#) WHEN MATCHED THEN UPDATE SET t.id=#id#,t.channel=#channel#,t.url=#url# WHEN NOT MATCHED THEN INSERT t_wlf_info (id,channel, url) VALUES(#id#,#channel#,#url#) </insert>
結果遇到了兩個問題:
1、java.sql.BatchUpdateException:ORA-00926: missing VALUES keyword
...
2、java. sql.BatchUpdateException: ORA-38104: Columns referenced in the ON Clause cannot be updated: "T"."ID"
...
第一個問題是在插入時多加了表明,在INSERT后面把表名去掉就好,另外提一點,update和insert后面都不需要加表明的,另外insert后面也不用加INTO,merge into的語法就是這樣的。第二個問題是不能把ON后面的條件字段放到update里,它認為你拿id和channel做條件來判斷是插入還是修改,如果匹配那么id和channel已經是相應的值了,就沒去修改了,舉例id=3 and channel=12345678時我去update,否則insert,那么匹配update時被修改的數據已經是id為3、channel為12345678了,這兩個字段就不用update了。
