Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:54:52+00:00 2026-06-08T04:54:52+00:00

I have a SQL statement which works when executed in MS Server Management Studio

  • 0

I have a SQL statement which works when executed in MS Server Management Studio and works when submitted from C# but which does not work when submitted from Java (1.6, using sqljdbc4.jar).

The basic problem seems to be selecting into a table variable. Following up on the first comment I’ve completely re-written this question using simpler examples to show what works and does not work.

The following query:

DECLARE @IsLoadRaw as INT = ? 
DECLARE @PrimaryID as varchar(1000) = ?
--Declare temporary table to hold message IDs and fill according to Primary ID.
DECLARE @MessageIDs TABLE ( MessageID BIGINT )
SELECT MessageID FROM Messages WHERE PrimaryID = @PrimaryID

works both in SQL Management Studio and when submitted from Java. In both cases it returns a result set with two MessageIDs (correct for the given PrimaryID I’m using to test).

The following query:

DECLARE @IsLoadRaw as INT = ? 
DECLARE @PrimaryID as varchar(1000) = ?
--Declare temporary table to hold message IDs and fill according to Primary ID.
DECLARE @MessageIDs TABLE ( MessageID BIGINT );
INSERT @MessageIDs SELECT MessageID FROM Messages WHERE PrimaryID = @PrimaryID;
SELECT * FROM @MessageIDs;

works in SQL Management Studio where it returns a result set with the same two MessageIDs. When submitted from Java it does not return any result set.

The complete statement, which makes us of @MessageIDs, works when submitted from C# via ADO.NET. I assume the second sample here would work as well. The problem is isolated to Java and seems to relate to using a table variable. Since the code appears correct and runs under SQL Management Studio I’m perplexed as to how to debug this.

Any idea why this is not working from Java? What tools can I use to understand what the server is doing with this query WHEN SUBMITTED FROM Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:54:54+00:00Added an answer on June 8, 2026 at 4:54 am

    I did some more excavation and found the answer:

    INSERT @MessageIDs SELECT MessageID FROM Messages WHERE PrimaryID = @PrimaryID;
    

    when submitted from Java returns an update count. When submitted from C# or from SQL Management Console it does not return an update count. I was not expecting this so it took some digging to find.

    The java API for stepping through results of an execute() this is confusing, there are not may examples, and at least one that I found was not fully correct. I’ll explain how I understand this to work.

    Since most statements are simple, one change or one select, there are convenience execute methods on Statement, such as executeQuerry(), which returns a result set. Most cases use these and that is the end of the story.

    If you have a more complex statement which does several things you call execute() and get back a list of things. INSERT, UPDATE, and DELETE (I believe) return a count of records modified. SELECT returns a result set. The result of executing a complex statement is a list of update counts and result sets, in the order they were executed. You then write code that steps through this list, processing each item.

    The statement

    DECLARE @MessageIDs TABLE ( MessageID BIGINT )
    INSERT @MessageIDs SELECT MessageID FROM Messages WHERE PrimaryID = @PrimaryID;
    SELECT * FROM Messages WHERE MessageID IN (SELECT MessageID FROM @MessageIDs) ORDER BY MessageID;
    SELECT * FROM Attrs WHERE MessageID IN (SELECT MessageID FROM @MessageIDs) ORDER BY MessageID;
    

    returns 2 result sets. In java, and only in java for reasons I don’t know, the INSERT @MessageIDs... statement returns an update count, which is the first item in the list.

    The java API for this is confusing. Statement.execute() and Statement.getMoreResults() return:

    • true if the next result is a ResultSet
    • false if the next result is an update count OR there are no more results

    false has two meanings and can not be interpreted to be the end of results. You have to also check for a non-zero update count.

    The final, functioning code ended up looking like this:

    List<DtaMessage> msgList = new ArrayList<DtaMessage>();
    boolean isResult = stmt.execute();
    
    // Skip over update counts.  
    while (!isResult) {
        if (stmt.getUpdateCount() == 0)
            // End of results.
            return msgList;
        isResult = stmt.getMoreResults();
    }
    
    
    // Process first result set.
    ResultSet rs = stmt.getResultSet();
    while (rs.next())
    {
        DtaMessage msg = PopulateMessage(rs, isLoadRaw);
        msgList.add(msg);
    }
    rs.close();
    
    // Skip over update counts.
    isResult = stmt.getMoreResults();
    while (!isResult) {
        if (stmt.getUpdateCount() == 0)
            // end of results.
            return msgList;
        isResult = stmt.getMoreResults();
    }
    
    // Process second result set.
    rs = stmt.getResultSet();
    while (rs.next())
    {
        // process.
    }
    rs.close();
    return msgList;
    

    Though my sample SQL does nothing that would generate an update count between the two result sets this method will process results from several different SQL statements so I added the code to skip over up date counts which may show up in some cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this statement in my SQL View (SQL Server 2008 R2) which
I have a VB6 application which works with datetime values in SQL Server (which
I'm not a DBA, but I have some basic understanding about how SQL Server
I have the following sql statement, which works perfect and since I'm transferring everything
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have a SQL statement which returns a number of rows and these are
I have the following SQL statement which has a very bad performance: SELECT frmInstLastModifiedDate
I have a fairly large SQL statement which has a number of inner joins
I have a method which includes sql statement . it is public Boolean addRSS(string
I have 3 tables which need to be linked in an SQL statement (I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.