diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 244cfb696..0e454226a 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -2210,7 +2210,14 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL jdbctype = ti.getSSType().getJDBCType().getIntValue(); } if (null != bcOperationColumnList && !bcOperationColumnList.isEmpty()) { - int columnIndex = bcOperationColumnList.indexOf(c.getColumnName()); + // find index ignore case + int columnIndex = -1; + for (int opi = 0; opi < bcOperationColumnList.size(); opi++) { + if (bcOperationColumnList.get(opi).equalsIgnoreCase(c.getColumnName())) { + columnIndex = opi; + } + } + if (columnIndex > -1) { columnMappings.put(columnIndex + 1, i); batchRecord.addColumnMetadata(columnIndex + 1, c.getColumnName(), jdbctype, diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java index ba3f6e70c..39e69c525 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java @@ -72,6 +72,8 @@ public class BatchExecutionTest extends AbstractTest { .escapeIdentifier(RandomUtil.getIdentifier("timestamptable1")); private static String timestampTable2 = AbstractSQLGenerator .escapeIdentifier(RandomUtil.getIdentifier("timestamptable2")); + private static String caseSensitiveTable = AbstractSQLGenerator + .escapeIdentifier(RandomUtil.getIdentifier("caseSensitiveTable")); /** * This tests the updateCount when the error query does cause a SQL state HY008. @@ -568,6 +570,27 @@ public void testBatchStatementCancellation() throws Exception { } } + @Test + public void testExecuteBatchColumnCaseMismatch() throws Exception { + // Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true + try (Connection con = DriverManager.getConnection(connectionString + + ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) { + try (Statement statement = con.createStatement()) { + TestUtils.dropTableIfExists(caseSensitiveTable, statement); + String createSql = "CREATE TABLE" + caseSensitiveTable + " (c1 varchar(10))"; + statement.execute(createSql); + } + // upper case C1 + try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + caseSensitiveTable + "(C1) VALUES(?)")) { + preparedStatement.setObject(1, "value1"); + preparedStatement.addBatch(); + preparedStatement.setObject(1, "value2"); + preparedStatement.addBatch(); + preparedStatement.executeBatch(); + } + } + } + /** * Get a PreparedStatement object and call the addBatch() method with 3 SQL statements and call the executeBatch() * method and it should return array of Integer values of length 3 @@ -839,6 +862,7 @@ private static void dropTable() throws SQLException { TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(ctstable4), stmt); TestUtils.dropTableIfExists(timestampTable1, stmt); TestUtils.dropTableIfExists(timestampTable2, stmt); + TestUtils.dropTableIfExists(caseSensitiveTable, stmt); } }