Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepared statement with PreparedStatement.RETURN_GENERATED_KEYS throws SQLServerException when invoked with SET NOCOUNT ON; #2587

Open
martinoconnor opened this issue Jan 18, 2025 · 0 comments

Comments

@martinoconnor
Copy link

Driver version

12.8.1.jre11

SQL Server version

Microsoft SQL Server 2022 (RTM-CU16) (KB5048033) - 16.0.4165.4 (X64) Nov 6 2024 19:24:49 Copyright (C) 2022 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 22.04.5 LTS)

Client Operating System

Linux - Fedora 41

JAVA/JVM version

openjdk 21.0.5 2024-10-15 LTS
OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode, sharing)

Table schema

CREATE TABLE [dbo].[test] (
[Id] INT IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED ([Id] ASC)
);

Problem description

The following code throws an exception:
try (Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=my_database;trustServerCertificate=true", "username", "password")) { PreparedStatement ps = con.prepareStatement("SET NOCOUNT ON; INSERT INTO dbo.test DEFAULT VALUES ;", PreparedStatement.RETURN_GENERATED_KEYS); ps.executeUpdate(); }

Expected behavior

I should be able to retrieve the generated ID

Actual behavior

Exception thrown

Error message/stack trace

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: A result set was generated for update.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:696)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:594)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7745)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:4391)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:276)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:246)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:537)
at Main.main(Main.java:11)

Any other details that can be helpful

State management of the ResultSet is incorrect if the update count is missing in the stream

JDBC trace logs

trace.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: To be triaged
Development

No branches or pull requests

1 participant