Skip to content

Conversation

@Ananya2
Copy link
Contributor

@Ananya2 Ananya2 commented Sep 10, 2025

Overview
This PR backports the fix from #2742 into the release/13.2 branch.

Problem:

  • PreparedStatement.getGeneratedKeys() was failing with "The statement must be executed before any results can be obtained" when INSERT statements had triggers

Root Cause:

  • PR #2737 correctly made SQLServerPreparedStatement.hasUpdateCountTDSTokenForInsertCmd() return false to fix update count issues for multi-statement queries
  • However, this change did not account for the generated keys use case where bRequestedGeneratedKeys is true
  • When hasUpdateCountTDSTokenForInsertCmd() returns false for PreparedStatement, it affects the TDS token processing in such a way that the generated keys ResultSet is not properly consumed or made available
  • When triggers fire and generated keys are requested, additional TDS tokens must be processed to locate the ResultSet containing the generated keys

Solution:

  • Modified hasUpdateCountTDSTokenForInsertCmd() to return true when generated keys are requested (bRequestedGeneratedKeys = true)
  • This maintains the correct update count behavior for multi-statement queries while preserving generated keys functionality
  • The conditional approach ensures both scenarios work correctly without breaking existing functionality

Testing:

  • Validates both update count accuracy and generated keys retrieval work correctly with triggers
  • Ensures multi-statement PreparedStatement queries still return correct update counts

…#2740) (#2742)

* Fix PreparedStatement getGeneratedKeys() failure with triggers (#2740)

* added test case
@Ananya2 Ananya2 merged commit b9c46d6 into release/13.2 Sep 11, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants