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

  • Home
  • SEARCH
  • 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 7888577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:52:02+00:00 2026-06-03T05:52:02+00:00

Is it possibile to count the number of result that a query returns using

  • 0

Is it possibile to count the number of result that a query returns using sp_executesql without executing query?
What I mean:

I have a procedure that gets a sql query in string.
Example:

SELECT KolumnaA FROM Users WHERE KolumnaA > 5

I would like to assign count of how many results this query will return, and store it in a variable, but I do not want to actually execute the query.

I cannot use this solution:

EXECUTE sp_executesql @sql          
SET @allCount = @@rowcount

because it returns the query result, in addition to getting the count of returned rows.

  • 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-03T05:52:03+00:00Added an answer on June 3, 2026 at 5:52 am

    I think there are two parts to the question, which I will address.

    The first part of the question is how to return row counts instead of the query results. This is done using Count(item). Using Count(1) instead of Count(KolumnaA) can be slightly faster, since it just counts the number of rows to be returned, instead of retreiving a specific column.

    SELECT Count(1) FROM Users WHERE KolumnaA > 5
    

    The second part is assigning this to a variable. If you need to use sp_executesql, you can do as follows:

    Declare @sql varchar(4000)
    Declare @allCount int
    Set @sql = 'SELECT 1 FROM Users WHERE KolumnaA > 5'
    sp_executesql(@sql)
    SET @allCount = @@rowcount 
    

    Alternatively, you can try to use the sp_executesql output feature:

    DECLARE @allCount int
    
    EXEC sp_executesql
       N'@allCount = SELECT Count(1) FROM Users WHERE KolumnaA > 5',
       '@allCount int OUTPUT',
       @allCount OUTPUT 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to count the number of columns as a result of Select Query. What
I have the following query that returns test questions, possible answers to those questions
I have two tables, one that stores serial number uut_build , and another for
I am using codeigniter and have working query which take user 3 images. I
So I have a MySQL query that is being created dynamically based on parameters
Possible Duplicate: How can I count the numbers of rows that a mysql query
As part of a reporting setup, I have a SQL query fetching the number
So I have a query where I select from the 'sites' table, and count
I have the following LINQ query, that is returning the results that I expect,
The Query Optimizer is estimating that the results of a join will have only

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.