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 7172321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:35:32+00:00 2026-05-28T15:35:32+00:00

I am using the Spring Framework’s StoredProcedure (I am extending it, of course) to

  • 0

I am using the Spring Framework’s StoredProcedure (I am extending it, of course) to get a result set and an output parameter (@totalRowsReturned) which is an Integer. The problem is that when the resultset being returned is supposed to be an empty list, I am getting a NullPointerException when I try to retrieve the output parameter (totalRows, which naively I would expect it to be zero).

I would like to mention that the code works fine when the result set being found is not empty.

My questions are:

  1. Why isn’t @totalRowsReturned being set to zero in this case? (Or in case it is, why can’t I retrieve it through the Java code?)

  2. How can I make this code (Java code + T-SQL code) work in such a way that @totalRowsReturned will be set to zero when required, and I could retrieve it through the Java code?

Dao:

List<Book> books = null;
int totalRows = 0;

Map<String, Object> results = storedProcedure.execute(parameters);
books = (List<Book>) results.get("rs");
totalRows = (Integer) results.get("totalRowsReturned"); // NullPointerException on this line if total rows are supposed to be zero!!

T-SQL stored procedure:

CREATE PROCEDURE Find_Books

    @authorName Varchar(250),   
    @totalRowsReturned INTEGER OUTPUT

AS

BEGIN

    SET NOCOUNT ON;
    DECLARE @SelectQuery NVARCHAR(2000)

    SET @SelectQuery = 'SELECT @totalRows=COUNT(*) OVER() FROM book b WHERE b.author_name = @authorName'

    Execute sp_Executesql @SelectQuery, N'@authorName VARCHAR(250), @totalRows int OUTPUT', @authorName, @totalRows=@totalRowsReturned OUTPUT

-- Select resultset goes here...

END

UPDATE:

Actually, my stored procedure looks more like this (the change is the additional @first_id = b.book_id in the SELECT):

CREATE PROCEDURE Find_Books

    @authorName Varchar(250),   
    @totalRowsReturned INTEGER OUTPUT

AS

BEGIN

    SET NOCOUNT ON;
    DECLARE @SelectQuery NVARCHAR(2000)

    DECLARE @first_id int
    DECLARE @first_id_local_returned int

    SET @SelectQuery = 'SELECT @first_id = b.book_id, @totalRows=COUNT(*) OVER() FROM book b WHERE b.author_name = @authorName'

    Execute sp_Executesql @SelectQuery, N'@authorName VARCHAR(250), @first_id int OUTPUT, @totalRows int OUTPUT', @authorName, @first_id=@first_id_local_returned OUTPUT, @totalRows=@totalRowsReturned OUTPUT

    -- Select resultset goes here... I am using the value of @first_id_local_returned in this SELECT...

END

The problem is, that when there are no rows returned from the SELECT, b.book_id is not defined, so I get an org.springframework.dao.TransientDataAccessResourceException ... Column 'book.book_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

So it seems like if I keep the OVER(), then @totalRows=COUNT(*) OVER() fails when there are zero rows returned, and if I remove the OVER(), then @first_id = b.book_id fails.

Any idea how I overcome this?

  • 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-05-28T15:35:32+00:00Added an answer on May 28, 2026 at 3:35 pm

    COUNT(*) OVER () is not the correct thing to use here. Just use COUNT(*)

    COUNT(*) OVER () returns a result set with as many rows as the COUNT. e.g if the result is 3 the result set will be

    3
    3
    3
    

    The effect of your query is then to repeatedly re-assign the value 3 to the @totalRows variable as many times as there are rows which is completely pointless.

    Conversely if COUNT(*) = 0 then the COUNT(*) OVER () result set is empty so your variable is never assigned to at all.

    COUNT(*) will always give you a single row scalar resultset here that you can assign to the variable and will have a more efficient execution plan without unnecessary common subexpression spools too.

    Edit

    In response to your question in the comments. This does the same thing as your linked article. It can use a narrower index to find the (say) 10,000th Employee then joins onto Department only for the 1 subsequent page of records. This paging method only works correctly because each employee has exactly one department.

    WITH E1(RN, EmployeeID)
         AS (SELECT ROW_NUMBER() OVER (ORDER BY EmployeeID),
                    EmployeeID
             FROM   Employees)
    SELECT TOP (@maximumRows) e.*,
                              d.Name AS DepartmentName
    FROM   Employees e
           INNER JOIN Departments d
             ON e.DepartmentID = d.DepartmentID
    WHERE  EmployeeID >= (SELECT EmployeeID
                          FROM   E1
                          WHERE  RN = @startRowIndex)
    ORDER  BY e.EmployeeID  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add the ability in my application which is using Spring framework
Our application is developed using Spring framework. Is it good practice to check null
We are developing a web application using Spring framework and Hibernate ORM. As far
I have a portlet application. It is configured using Spring framework IoC container. I
I'm developing an Android 3.1 and above application. I'm using Spring Framework 1.0.0.0RC1 for
For my web application, I am thinking about using the Spring framework. Initially, I
I have this web application using Spring Web Flow framework. In my main page
We're securing Mule services using the Spring Security Framework, and some of the services
I'm using JPA in my DAOs outside of Spring . The Spring framework defines
I'm using hibernate validator framework with Spring. A class implementing the Spring Validator validates

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.