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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:06:46+00:00 2026-06-11T03:06:46+00:00

I have a stored procedure which generates and executes a piece of dynamic T-SQL

  • 0

I have a stored procedure which generates and executes a piece of dynamic T-SQL which, once built up, looks like this

SELECT 
    tblUsers.strUserName AS [Username]
    ,tblUsers.strEmail AS [Email]
    ,tblUserAuditLog.strIpAddress AS [IP Address]
    ,tblUserAuditLog.dtAuditTimeStamp AS [Timestamp]
    ,tblUserAuditLog.strAuditLogAction AS [Action]
    ,tblUserAuditLog.strLogDetails AS [Details]
FROM         
    tblUserAuditLog 
        LEFT OUTER JOIN tblUsers 
        ON tblUserAuditLog.intUserIdFK = tblUsers.intUserId
WHERE 
    tblUsers.strUserName = 'a12jun'
    AND tblUserAuditLog.dtAuditTimeStamp >= '2012-08-10'

This query can return several thousand rows in the dev environment and will return considerably more in live.

I want to find out how many rows the dynamic query returns before I actually return the results, so that if the number is more than some limit, I can return a ‘narrow your query’ error message.

I have tried generating another piece of SQL like this:

DECLARE @sqlrowcount NVARCHAR(MAX);
SET @sqlrowcount = 'SELECT COUNT(*) FROM (' + @sql + ') AS TEMP';
EXEC(@sqlrowcount);

IF @@ROWCOUNT > @limit BEGIN .... END

where @sql is the dynamic query. I then embarrassingly realised that EXEC(@sqlrowcount) will always return 1, because it returns one record whose value is the number of records.

Is there a (relatively) elegant way of doing this, e.g. without writing the result to a temporary table?

  • 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-11T03:06:47+00:00Added an answer on June 11, 2026 at 3:06 am

    One way;

    --base sql
    declare @sql  nvarchar(255) = N'select * from master.dbo.spt_values'
    
    --count wrapper
    declare @sqlb nvarchar(255) = N'set @count=(select count(*) from (' + @sql + ') T)'
    
    declare @count int
    exec sp_executesql @sqlb, N'@count int output', @count output
    
    select 'rows=',@count
    

    You could also use TOP to enforce a limit, running the same statement twice is not very efficient.

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

Sidebar

Related Questions

I have a stored procedure which basically does something like select top 1 expiryDate,
I have a stored procedure which executes another procedure inside it. The second one
I have a stored procedure in which I would like to return a result
I have the following stored procedure which will generate mon to sun and then
I have a stored procedure which update a table based on such calculation and
I have a stored procedure which returns sys_refcursor and I am trying to fetch
I have a stored procedure which accepts a User defined table type called SeasonTable
I have a stored procedure which takes an XML parameter and inserts the data
I have a stored procedure which returns a ref cursor as follows: CREATE OR
I have a stored procedure which is doing a lot of delete. Hundreds of

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.