DB2 单张表 merge语句

<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);

	}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。