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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:36:24+00:00 2026-06-06T06:36:24+00:00

This may be an easy answer but I’ve been staring at it for too

  • 0

This may be an easy answer but I’ve been staring at it for too long…

I have the following query that takes a stored procedure input parameter as a variable name and counts the records in that table. I’d like to retrieve the results of the dynamic statement (@toStartStr) into a variable (@toStart).

-- @tempTableName = SProc input parameter
DECLARE @toStartStr nvarchar(150);
DECLARE @toStart int;
SET @toStartStr = 'SELECT @toStart = COUNT(ID) FROM ' + @tempTableName;
EXEC(@toStartStr);

Right now, an error suggests that @toStart cannot be concatenated with the string SELECT, but this is the gist of what I want. Can anyone see what I’m doing wrong? Or suggest an alternative? FYI SQL 2008 R2. Thanks.

  • 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-06T06:36:26+00:00Added an answer on June 6, 2026 at 6:36 am
    DECLARE @sql NVARCHAR(255);
    
    DECLARE @toStart INT;
    
    SET @sql = N'SELECT @toStart = COUNT(ID) FROM ' + QUOTENAME(@tempTableName);
    
    EXEC sp_executesql @sql, N'@toStart INT OUTPUT', @toStart OUTPUT;
    
    PRINT @toStart;
    

    However there is a much easier and more efficient way to do this, if you’re okay with ignoring current in-flight transactions (and you’re using SQL Server 2005 or better – please specify the version when asking questions!).

    DECLARE @toStart INT;
    
    SELECT @toStart = SUM(rows) 
      FROM sys.partitions
      WHERE [object_id] = OBJECT_ID(@tempTableName)
      AND index_id IN (0,1);
    
    PRINT @toStart;
    

    Just for completeness, here is a solution for SQL Server 2000, which also doesn’t require any special privileges (just connect and member of public):

    DECLARE @toStart INT;
    
    SELECT @toStart = [rows] 
      FROM sysindexes
      WHERE id = OBJECT_ID(@tempTableName)
      AND indid IN (0,1);
    
    PRINT @toStart;
    

    That said, if you’re using a count to determine what the next ID might be, or something like that, I think you’re approaching this the wrong way, since rows can be deleted and if it’s an identity column values can be skipped due to rollbacks.

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

Sidebar

Related Questions

This may be a stupid question but I have a code with the following
I have a feeling I may already know the answer to this question, but
This may be an easy question to answer but I could not find a
I know this may be an easy answer to some of you but i
This question may be easy and the answer obvious, but I can't seem to
This sounds like a pretty easy question to answer but I haven't been able
Well this may be a very easy Question but I'm not getting the answer
I am fairly new to OSX development, so this may be easy but google
This may be super easy to solve but I can't find the correct google
This may seem an easy question, but not to me, also a search has

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.