在Oracle中,可以使用merge語(yǔ)句實(shí)現(xiàn)對(duì)表的更新或插入。語(yǔ)法格式如下:
merge into tableName using tableName on(join_condition) when matched then update set...
when not matched then insert(...) values(...)
這個(gè)語(yǔ)句的意思是把using表合并到into表,合并條件是on(condition),當(dāng)條件滿足時(shí)只能是更新into表中的對(duì)應(yīng)的記錄,當(dāng)條件不滿足時(shí),則也只能是往into表里面添加對(duì)應(yīng)的數(shù)據(jù),而該數(shù)據(jù)中也只能使用using表中當(dāng)前記錄對(duì)應(yīng)的數(shù)據(jù)。
示例如下:
假設(shè)有一個(gè)student表,那么以下語(yǔ)句就可以實(shí)現(xiàn)當(dāng)a的id大于b的id的時(shí)候把所有student的年齡加2,否則就新增一條記錄。
merge into student a using student b on(a.id>b.id) when matched then update set age=age+2 when not matched then insert(id,name,age,sex,no)
values(b.id+100,b.name,b.age,b.sex,b.no);
新聞熱點(diǎn)
疑難解答
圖片精選