I have a table like this in my Azure SQL Data Warehouse database:
CREATE TABLE t_identity (
id INTEGER IDENTITY (1, 1) NOT NULL,
val INTEGER
)
Now, using JDBC, I want to insert a row and fetch the generated identity value. This would work in SQL Server and most other databases:
try (Connection c = DriverManager.getConnection(url, properties);
Statement s = c.createStatement()) {
s.executeUpdate("insert into t_identity (val) values (1)",
Statement.RETURN_GENERATED_KEYS);
try (ResultSet rs = s.getGeneratedKeys()) {
while (rs.next())
System.out.println(rs.getObject(1));
}
}
But on SQL Data Warehouse, it doesn't work:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 'SCOPE_IDENTITY' is not a recognized built-in function name.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:264)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1585)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:876)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:776)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7385)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2750)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:235)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:210)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:2060)
at SQLServer.main(SQLServer.java:65)
The other methods (e.g. executeUpdate(String, String[])
) don't work either, as they delegate to the above one.
I understand that SQL Data Warehouse doesn't support the SCOPE_IDENTITY()
and similar functions, which seem to be used behind the scenes by the mssql-jdbc driver:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.5.2.jre8-preview</version>
</dependency>
But is there a workaround?
Notes:
- The output clause is also not available: https://learn.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql
- I've also filed a bug on Github for mssql-jdbc