码迷,mamicode.com
首页 > 数据库 > 详细

DB2 单张表 merge语句

时间:2015-04-21 11:14:37      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:db2   merger   

<span style="font-family: Arial, Helvetica, sans-serif;">merge into CHANNEL_AGENT_PEOPLE_NUMBER_201503 a using (SELECT  ? TIME_ID,? CITY_NAME,? NODE_NAME,? NODE_CODE,? PEOPLE_NUMBER, ? VEHICLE_RUNNING_COSTS,? MAIL_TRANSPORT_FEES,? TRAINING_COSTS, ? CONSUMABLES,? OTHER_OFFICE_EXPENSES FROM SYSIBM.SYSDUMMY1) B  on a.time_id = b.time_id and a.CITY_NAME= b.CITY_NAME AND  A.NODE_NAME = B.NODE_NAME AND A.NODE_CODE = B.NODE_CODE WHEN MATCHED THEN  update  set a.PEOPLE_NUMBER = b.PEOPLE_NUMBER, A.VEHICLE_RUNNING_COSTS =B.VEHICLE_RUNNING_COSTS, A.MAIL_TRANSPORT_FEES= B.MAIL_TRANSPORT_FEES, A.TRAINING_COSTS = B.TRAINING_COSTS, A.CONSUMABLES = B.CONSUMABLES, A.OTHER_OFFICE_EXPENSES = B.OTHER_OFFICE_EXPENSES WHEN NOT MATCHED THEN  INSERT   VALUES( B.TIME_ID,B.CITY_NAME, B.NODE_NAME,B.NODE_CODE,B.PEOPLE_NUMBER,B.VEHICLE_RUNNING_COSTS, B.MAIL_TRANSPORT_FEES,	B.TRAINING_COSTS,B.CONSUMABLES,B.OTHER_OFFICE_EXPENSES)</span>


今天在开发中用到了单张表做merge语句,使用spring jdbctemplate的批量修改,所以要传递参数进去,使用了上面的merge语句完成,在此做个记录。

部分java代码如下:

private void uploadAgentChannel(Workbook workbook, String city, int month) {
		Sheet sheet = workbook.getSheetAt(0);
		List<Object[]> params = new ArrayList<Object[]>();
		for (int i = 3; i < sheet.getLastRowNum(); i++) {
			params.add(new Object[] { month,
					sheet.getRow(i).getCell(0).getStringCellValue(),
					sheet.getRow(i).getCell(1).getStringCellValue(),
					sheet.getRow(i).getCell(2).getStringCellValue(),
					sheet.getRow(i).getCell(3).getNumericCellValue(),
					sheet.getRow(i).getCell(4).getNumericCellValue(),
					sheet.getRow(i).getCell(5).getNumericCellValue(),
					sheet.getRow(i).getCell(6).getNumericCellValue(),
					sheet.getRow(i).getCell(7).getNumericCellValue(),
					sheet.getRow(i).getCell(8).getNumericCellValue() });
		}
		// 要判断对应的月表是否存在,不存在就创建表

		if (!isTableExists("CHANNEL_AGENT_PEOPLE_NUMBER", month)) {
			jdbcTemplate.execute("create table CHANNEL_AGENT_PEOPLE_NUMBER_"
					+ month + " like CHANNEL_AGENT_PEOPLE_NUMBER");
		}
		// 使用merge语句可以修改和插入对应数据
		String sql = "merge into CHANNEL_AGENT_PEOPLE_NUMBER_"
				+ month
				+ " a using (SELECT "
				+ " ? TIME_ID,? CITY_NAME,? NODE_NAME,? NODE_CODE,? PEOPLE_NUMBER,"
				+ " ? VEHICLE_RUNNING_COSTS,? MAIL_TRANSPORT_FEES,? TRAINING_COSTS,"
				+ " ? CONSUMABLES,? OTHER_OFFICE_EXPENSES FROM SYSIBM.SYSDUMMY1) B "
				+ " on a.time_id = b.time_id and a.CITY_NAME= b.CITY_NAME AND "
				+ " A.NODE_NAME = B.NODE_NAME AND A.NODE_CODE = B.NODE_CODE"
				+ " WHEN MATCHED THEN  update  set a.PEOPLE_NUMBER = b.PEOPLE_NUMBER,"
				+ " A.VEHICLE_RUNNING_COSTS =B.VEHICLE_RUNNING_COSTS,"
				+ " A.MAIL_TRANSPORT_FEES= B.MAIL_TRANSPORT_FEES,"
				+ " A.TRAINING_COSTS = B.TRAINING_COSTS, A.CONSUMABLES = B.CONSUMABLES,"
				+ " A.OTHER_OFFICE_EXPENSES = B.OTHER_OFFICE_EXPENSES"
				+ " WHEN NOT MATCHED THEN  INSERT   VALUES( B.TIME_ID,B.CITY_NAME,"
				+ " B.NODE_NAME,B.NODE_CODE,B.PEOPLE_NUMBER,B.VEHICLE_RUNNING_COSTS,"
				+ " B.MAIL_TRANSPORT_FEES,"
				+ "	B.TRAINING_COSTS,B.CONSUMABLES,B.OTHER_OFFICE_EXPENSES)";
		jdbcTemplate.batchUpdate(sql, params);

	}


DB2 单张表 merge语句

标签:db2   merger   

原文地址:http://blog.csdn.net/zml6308/article/details/45166955

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!