Oracle中批量执行插入的方法

批量插入在oracle中的写法:

INSERT ALL INTO USER  (ID,NAME,AGE,ROLE)
       INTO USER (ID,NAME,AGE,ROLE) VALUES  (SYS_GUID() ,'张三', '18' ,'学生' )
       INTO USER (ID,NAME,AGE,ROLE) VALUES  (SYS_GUID() ,'李四', '18' ,'学生' )
       INTO USER (ID,NAME,AGE,ROLE) VALUES  (SYS_GUID() ,'王五', '28' ,'学生' )
       SELECT SYS_GUID() ,'孙六', '19' ,'学生' FROM DUAL;

需要注意的是, 最后必须加SELECT语句!

集成mybatis之后,需要用循环的方式遍历出INTO语句,注意最后的SELECT;

@Insert({
	"<script>",
	" INSERT ALL INTO GGS_FLOW_CONFIG_NOTICE_ROLE (ID,NOTICEID,NAME,ROLE)  ",
		"<foreach collection='list' item='item' index='index' separator=''>",
		// oracle语句特性, 最后一个要从select中查询
		"<choose> ",
			"<when test='index == (list.size() - 1)'> " +
				" SELECT SYS_GUID() ,#{noticeid}, #{item.name} ,#{item.id} FROM DUAL ",
			"</when> ",
			// 其他的写into语句
			"<otherwise>" +
				" INTO GGS_FLOW_CONFIG_NOTICE_ROLE (ID,NOTICEID,NAME,ROLE) VALUES  (SYS_GUID() ,#{noticeid}, #{item.name} ,#{item.id} ) ",
			"</otherwise> " ,
		"</choose>",
		"</foreach>",
	"</script>"
	})
	int doInsertNoticeRoleBatch(@Param(value="noticeid") String noticeid ,@Param(value="list") List<FlowConfigNoticeRole> list);

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×