diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 2128f9cef..244cfb696 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -1246,16 +1246,25 @@ public final java.sql.ResultSetMetaData getMetaData() throws SQLServerException, */ private SQLServerResultSet buildExecuteMetaData() throws SQLServerException, SQLTimeoutException { String fmtSQL = userSQL; - + SQLServerResultSet emptyResultSet = null; try { - fmtSQL = replaceMarkerWithNull(fmtSQL); internalStmt = (SQLServerStatement) connection.createStatement(); emptyResultSet = internalStmt.executeQueryInternal("set fmtonly on " + fmtSQL + "\nset fmtonly off"); } catch (SQLServerException sqle) { // Ignore empty result set errors, otherwise propagate the server error. if (!sqle.getMessage().equals(SQLServerException.getErrString("R_noResultset"))) { - throw sqle; + //try by replacing ? characters in case that was an issue + try { + fmtSQL = replaceMarkerWithNull(fmtSQL); + internalStmt = (SQLServerStatement) connection.createStatement(); + emptyResultSet = internalStmt.executeQueryInternal("set fmtonly on " + fmtSQL + "\nset fmtonly off"); + } catch (SQLServerException ex) { + // Ignore empty result set errors, otherwise propagate the server error. + if (!ex.getMessage().equals(SQLServerException.getErrString("R_noResultset"))) { + throw ex; + } + } } } return emptyResultSet; diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java index 23c89071d..87e0994aa 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java @@ -13,8 +13,10 @@ import java.lang.reflect.Field; import java.sql.BatchUpdateException; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; @@ -124,6 +126,25 @@ public void testPreparedStatementWithSpPrepare() throws SQLException { } } } + + @Test + void testDatabaseQueryMetaData() throws SQLException { + try (Connection connection = getConnection()) { + try (SQLServerPreparedStatement stmt = (SQLServerPreparedStatement) connection.prepareStatement( + "select 1 as \"any questions ???\"")) { + ResultSetMetaData metaData = stmt.getMetaData(); + String actualLabel = metaData.getColumnLabel(1); + String actualName = metaData.getColumnName(1); + + String expected = "any questions ???"; + assertEquals(expected, actualLabel, "Column label should match the expected value"); + assertEquals(expected, actualName, "Column name should match the expected value"); + } + } catch (SQLException e) { + e.printStackTrace(); + fail("SQLException occurred during test: " + e.getMessage()); + } + } @Test public void testPreparedStatementParamNameSpacingWithMultipleParams() throws SQLException { @@ -927,5 +948,5 @@ private static void dropTables() throws Exception { TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName5), stmt); } } - + }