diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java index ae98a220c..fe89eb806 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDatabaseMetaData.java @@ -2537,6 +2537,27 @@ public boolean locatorsUpdateCopy() throws SQLException { checkClosed(); return true; } + + /* -------------- MSSQL-JDBC Extension methods start here --------------- */ + + /** + * Returns the database compatibility level setting for the current database. This is useful if the + * database's compatibility level is lower than the engine version. In this case the database will only + * support SQL commands at its compatibility level, and not the wider set of commands accepted by the + * engine. + * + * @return the database compatibility level value (from sys.databases table). + */ + public int getDatabaseCompatibilityLevel() throws SQLException { + checkClosed(); + String database = connection.getCatalog(); + SQLServerResultSet rs = getResultSetFromInternalQueries(null, + "select name, compatibility_level from sys.databases where name = '" + database + "'"); + if (!rs.next()) { + return 0; + } + return rs.getInt("compatibility_level"); + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java index 21e382705..ebe0eb25d 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java @@ -120,6 +120,15 @@ public void testDriverVersion() throws SQLException, IOException { } } + @Test + public void testDatabaseCompatibilityLevel() throws SQLException { + try (Connection conn = getConnection()) { + SQLServerDatabaseMetaData dbmData = (SQLServerDatabaseMetaData) conn.getMetaData(); + int compatibilityLevel = dbmData.getDatabaseCompatibilityLevel(); + assertTrue(compatibilityLevel > 0); + } + } + /** * Your password should not be in getURL method. *