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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:19:51+00:00 2026-05-24T00:19:51+00:00

In a SQL Server 2008R2 sproc, I have the following code to calculate the

  • 0

In a SQL Server 2008R2 sproc, I have the following code to calculate the total number of records returned by a dynamic query:

        -- count the actual number of results
        DECLARE @rowcount TABLE (Value int);
        INSERT INTO @rowcount
        EXEC('SELECT COUNT(*) ' + @sqlBody);
        SELECT @varcharActualNumResults = VALUE FROM @rowcount;

The dynamic query is broken up into three parts: @sqlTop, @sqlBody, and @sqlBottom.

I want to know how many records are actually returned by @sqlBody, but the actual execution is the concatenated @sqlTop + @sqlBody + @sqlBottom.

The issue is that the above query takes 24136ms (roughly 24 seconds) where the actual number of records is around 18,000.

Another issue comes up where I want to get the rowcount for the entire query:

         EXEC (@sqlTop + @sqlBody + @sqlBottom)
         SET @NumberOfResultsReturned = @@ROWCOUNT;

This execution takes roughly two seconds.

Here is a sample query:

SELECT TOP(10) Title
FROM ItemData 
WHERE  ( 
           FREETEXT(Title, '"windshield"')  
           OR 
           (   [Title] LIKE '%mazda 6%' )  
       )   
AND  ( WebsiteID=1 )  
ORDER BY DateAdded DESC 

@sqlBody contains everything between the FROM and before ORDER BY.

How can this be optimized?

UPDATE

In my sproc I defined my temp table then attempt to populate it through the EXEC command. However at the EXEC command line I’m getting an error indicating the temp table has to be defined:

DECLARE @Temp_Results TABLE (ItemListID BIGINT, Title VARCHAR(255) )
EXEC ('INSERT INTO @Temp_Results (ItemListID, Title) (' + @sqlTop + @sqlBody + ')')

If I take out the exec command and execute the SQL it works fine. How can I get around 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-24T00:19:52+00:00Added an answer on May 24, 2026 at 12:19 am

    I understand your problem now and I think the best way is to not use a dynamic query. However, if you have to use the dynamic query here are some ways to speed it up.

    1. Make sure all the items in the where have indexes.

    2. Add your counting into the EXEC so that the row count is returned with one exec call. For example in your dynamic query put the results of the reduction in a temp table and count that table then select from that table.

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

Sidebar

Related Questions

SQL Server (2005/2008) Each of the below statements have the same result. Does anyone
SQL Server procedure can return result sets. I have a table emp(emp__id, emp__name, ...)
SQL Server Version 2000. We've a bunch of desktops talking to MSSQL Server. When
SQL Server 2000 Standard, Windows 2003 My coworker removed 'BUILTIN\Administrators' group from SQL Server
SQL Server 2000 doesn't seem to support the xml based .xlsx file types. Besides
SQL Server 2005/2008 Express edition has the limitation of 4 GB per database. As
SQL Server profiler is great for profiling SQL Server performance for web apps. However,
Sql Server Management Studio 2008 is not scripting table permissions even when I select
SQL Server 2000: Is there a way to find out server memory / CPU
SQL Server 2005. I'm adding Foreign Key constraints to the database of an application

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.